LoginSignup
0
0

More than 1 year has passed since last update.

AWS Lambda(node.js)でyoutube APIを利用してJSON作成した話

Last updated at Posted at 2023-02-01

どんな記事

・AWSを使ってyoutube APIにアクセスするLambda関数を作成したときのお話
 node.jsの作成例が少なかったのでこんな感じでうまくいったよが伝われば・・

利用技術

・AWS Lambda
・AWS S3
・youtube-node(youtube API) ※google developerに登録する
・node.js 16

前提条件

・node.js 16での開発

パッケージのインストール

 npm install youtube-node
 npm install aws-sdk

作成したコード(抜粋)

const AWS = require('aws-sdk');
const s3 = new AWS.S3({'region':'<地域>'});

const Youtube = require('youtube-node');
const youtube = new Youtube();

exports.handler = function(event, context, callback) {

    // キーワードはLambdaの引数に設定(json形式)
    var keyword = event['q'];
    var limit = 50;

    youtube.setKey('<developer登録したときにゲットしたkey>');

    // 細かい設定 (youtube API仕様書参照)
    youtube.addParam('order', 'date');
    youtube.addParam('type', 'video');
    youtube.addParam('regionCode', 'JP');

    // 検索の実行
    youtube.search(keyword, limit, function(err, result) {
        items = result["items"];
        for (var i in items) {
            //検索結果からjsonを新しく作成する
            //作成したjsonをS3へ配置する(s3.putObject)
        }
    })
};

作業内容

1.AWS Lambdaで関数の作成(任意の名前)

2.作成したコードをアップロード(zipでOK)

3.キーワードをイベントJSONに追加してテスト

{
  "q": "検索キーワード"
}

4.テスト結果成功!
image.png

まとめ

youtube API 自体がkeyさえあればインターネットを通してアクセスが可能なので結構簡単にできた。
youtubeに関してLambdaの参照の設定などは特に必要なかった。
※S3に配置するための関数の許可の設定は必要。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0