aws(学习笔记第十七课) SQS Amazon Simple Queue Service服务
aws(学习笔记第十七课)
学习内容:
使用SQS Amazon Simple Queue Service
服务 整体代码(nodejs
的通常工程) 代码动作
1. 使用SQS Amazon Simple Queue Service
服务
利用应用程序来学习SQS
整体代码(nodejs
的通常工程) config.json
将QueueUrl
替换成上面创建的SQS
将Bucket
替换成上面创建的S3 bucket
的名字{ "QueueUrl" : "https://sqs.ap-northeast-1.amazonaws.com/081353481087/url2png" , "Bucket" : "urs2png-20241211-finlay"
}
package.json
定义依赖关系{ "dependencies" : { "aws-sdk" : "2.1.18" , "node-uuid" : "1.4.3" , "node-webshot" : "1.0.4" , "phantomjs-prebuilt" : "2.1.16" } , "private" : true
}
index.js
(生产者 producer
) it send url of png to sqs on aws.var AWS = require ( 'aws-sdk' ) ;
var uuid = require ( 'node-uuid' ) ;
var config = require ( './config.json' ) ;
var sqs = new AWS. SQS ( { "region" : "ap-northeast-1"
} ) ; if ( process. argv. length !== 3 ) { console. log ( 'URL missing' ) ; process. exit ( 1 ) ;
} var id = uuid. v4 ( ) ;
var body = { "id" : id, "url" : process. argv[ 2 ]
} ; sqs. sendMessage ( { "MessageBody" : JSON . stringify ( body) , "QueueUrl" : config. QueueUrl
} , function ( err ) { if ( err) { console. log ( 'error' , err) ; } else { console. log ( 'PNG will be soon available at http://' + config. Bucket + '.s3-website-us-east-1.amazonaws.com/' + id + '.png' ) ; }
} ) ;
worker.js
消费者(consumer
) receive message of sqs on aws every 1 second.var fs = require ( 'fs' ) ;
var AWS = require ( 'aws-sdk' ) ;
var webshot = require ( 'node-webshot' ) ;
var config = require ( './config.json' ) ;
var sqs = new AWS. SQS ( { "region" : "us-east-1"
} ) ;
var s3 = new AWS. S3 ( { "region" : "us-east-1"
} ) ; function acknowledge ( message, cb ) { var params = { "QueueUrl" : config. QueueUrl, "ReceiptHandle" : message. ReceiptHandle} ; sqs. deleteMessage ( params, cb) ;
} function process ( message, cb ) { var body = JSON . parse ( message. Body) ; var file = body. id + '.png' ; webshot ( body. url, file, function ( err ) { if ( err) { cb ( err) ; } else { fs. readFile ( file, function ( err, buf ) { if ( err) { cb ( err) ; } else { var params = { "Bucket" : config. Bucket, "Key" : file, "ACL" : "public-read" , "ContentType" : "image/png" , "Body" : buf} ; s3. putObject ( params, function ( err ) { if ( err) { cb ( err) ; } else { fs. unlink ( file, cb) ; } } ) ; } } ) ; } } ) ;
} function receive ( cb ) { var params = { "QueueUrl" : config. QueueUrl, "MaxNumberOfMessages" : 1 , "VisibilityTimeout" : 120 , "WaitTimeSeconds" : 10 } ; sqs. receiveMessage ( params, function ( err, data ) { if ( err) { cb ( err) ; } else { if ( data. Messages === undefined ) { cb ( null , null ) ; } else { cb ( null , data. Messages[ 0 ] ) ; } } } ) ;
} function run ( ) { receive ( function ( err, message ) { if ( err) { throw err; } else { if ( message === null ) { console. log ( 'nothing to do' ) ; setTimeout ( run, 1000 ) ; } else { console. log ( 'process' ) ; process ( message, function ( err ) { if ( err) { throw err; } else { acknowledge ( message, function ( err ) { if ( err) { throw err; } else { console. log ( 'done' ) ; setTimeout ( run, 1000 ) ; } } ) ; } } ) ; } } } ) ;
} run ( ) ;
3. 代码动作
安装nodejs
由于一些包的原因,这个nodejs
程序只能运行在nodejs@v11.15.0
,因此事先下载。 nodejsv11.15.0 执行npm install
npm install
准备图片 示例图片URL 运行程序
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/490923.html
如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!