aws(学习笔记第十七课) SQS Amazon Simple Queue Service服务

aws(学习笔记第十七课)

  • SQS Amazon Simple Queue Service服务

学习内容:

  • 使用SQS Amazon Simple Queue Service服务
  • 整体代码(nodejs的通常工程)
  • 代码动作

1. 使用SQS Amazon Simple Queue Service服务

  1. 利用应用程序来学习SQS
    • 创建S3

      shell 复制代码
      $ aws s3 mb s3://urs2png-20241211-finlay
      $ aws s3 website s3://urs2png-20241211-finlay --index-document index.html --error-document error.html
    • 创建SQS

      shell 复制代码
      aws sqs create-queue --queue-name url2png
  2. 整体代码(nodejs的通常工程)
    • config.json
      QueueUrl替换成上面创建的SQS
      Bucket替换成上面创建的S3 bucket的名字

      json 复制代码
      {
      	"QueueUrl": "https://sqs.ap-northeast-1.amazonaws.com/081353481087/url2png",
      	"Bucket": "urs2png-20241211-finlay"
      }
    • package.json 定义依赖关系

      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.

      json 复制代码
      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.

      javascript 复制代码
      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. 代码动作

  1. 安装nodejs
    由于一些包的原因,这个nodejs程序只能运行在nodejs@v11.15.0,因此事先下载。
    nodejsv11.15.0

  2. 执行npm install

    shell 复制代码
    npm install
  3. 准备图片
    示例图片URL

  4. 运行程序

    • 首先运行work.js监视aws sqs

      shell 复制代码
      node work.js
    • 之后运行index.js

      shell 复制代码
      node index.js "https://pics1.baidu.com/feed/f9dcd100baa1cd11d2771efce7c9a6f8c1ce2d0c.jpeg@f_auto?token=e630acf36ce316be5abcb2652d6971b3&s=110AA3F04A32B6C842AFE716030040DF"


      可以看到work.js这边显示done,表明已经接收到urlmessage,处理成文件之后传送到了S3 bucket

    • 最后检查S3 bucket

相关推荐
快下雨了L4 小时前
C++面试笔记(持续更新...)
笔记
AWS官方合作商5 小时前
Amazon Lex:AI对话引擎重构企业服务新范式
人工智能·ai·机器人·aws
陈无左耳、5 小时前
HarmonyOS学习第3天: 环境搭建开启鸿蒙开发新世界
学习·华为·harmonyos
柃歌5 小时前
【UCB CS 61B SP24】Lecture 7 - Lists 4: Arrays and Lists学习笔记
java·数据结构·笔记·学习·算法
JANGHIGH5 小时前
c++ std::list使用笔记
c++·笔记·list
柃歌5 小时前
【UCB CS 61B SP24】Lecture 4 - Lists 2: SLLists学习笔记
java·数据结构·笔记·学习·算法
大溪地C6 小时前
Git 合并冲突解决与状态分析笔记
笔记·git
虾球xz6 小时前
游戏引擎学习第115天
学习·游戏引擎
BUG 劝退师7 小时前
C语言预处理学习笔记
c语言·笔记·学习
Chambor_mak7 小时前
stm32单片机个人学习笔记16(SPI通信协议)
stm32·单片机·学习