Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

Organization

S3にファイルが置かれた時 複数のSQS にエンキューする方法

はじめに

yaml が S3 に置かれた時それをトリガにSQSにエンキューする必要がありました。
一般的には、S3 Event Notificasions によりエンキューをすれば良いのですが、エンキュー先が複数に必要になりました。

用途別で分けるだけなら、S3のプレフィックスで制御することもできますが、時間帯により分ける必要があったので、lambda を使って実装しました。

エンキュー先

  1. 夜間用SQS(process.env.sqsUriNight)
  2. 昼の低速処理用SQS(process.env.sqsUriNormal)
  3. 昼の高速処理用SQS(process.env.sqsUriFast)

使ったコード

低速高速の分岐はコードを見ての通り、 yaml 内にある photos の数により判断します。
同様のソースを書く場合の参考にしてください。

index.js(node8)
'use strict';

const aws = require('aws-sdk');
const yaml = require('yaml');
require('date-utils');

const s3 = new aws.S3({apiVersion: '2006-03-01'});
const sqs = new aws.SQS({apiVersion: '2012-11-05'});
let sqsUriNormal = process.env.sqsUriNormal;
let sqsUriFast = process.env.sqsUriFast;
let sqsUriNight = process.env.sqsUriNight;
let threshold = process.env.threshold;
let nightStart = process.env.nightStart;
let nightEnd = process.env.nightEnd;

let enqueueSQS = (uri, key) => {
  let params = {
    MessageBody: key,
    QueueUrl: uri,
  };

  return sqs.sendMessage(params).promise();
};

let getYaml = (bucket, key) => {
  let yamlParam = {
    Bucket: bucket,
    Key: key,
  };

  return s3.getObject(yamlParam).promise();
};

let isNight = () => {
  let now = Number(new Date().toFormat('HH24MISS'));
  return nightStart <= now && now < nightEnd;
};

let parseYaml = (data) => yaml.parse(data.Body.toString());

let getPutSqs = (data) => {
  if (isNight()) {
    return sqsUriNight;
  }
  if (data.photos.length > threshold) {
    return sqsUriNormal;
  }
  return sqsUriFast;
};

exports.handler = async (event, context) => {
  let bucket = event.Records[0].s3.bucket.name;
  let key = decodeURIComponent(event.Records[0].s3.object.key);
  try {
  let sqsName = getPutSqs(parseYaml(await getYaml(bucket, key)));
  let ret = await enqueueSQS(sqsName, key);
  console.log(msg);
  return msg;
};

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?