Created
January 30, 2017 18:14
-
-
Save baodinh/eda94e62a34b9049244a80e55b337ef3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require("aws-sdk"); | |
AWS.config.update({ | |
accessKeyId: "", | |
secretAccessKey: "", | |
region: "us-west-2" | |
}); | |
var s3 = new AWS.S3(); | |
var bucketName = "movieinfos3"; | |
exports.handler = function (event, context, callback) { | |
//check if parameter is invalid | |
if (event.movieId == null) { | |
callback(null, "Invalid parameter"); | |
} | |
//declare config for each movie | |
var params = { | |
Bucket: bucketName, /* required */ | |
Prefix: event.movieId | |
}; | |
//declare config for getting signedUrl Image from S3 | |
var signedUrlParams = { | |
Bucket: bucketName | |
}; | |
var arrImageUrl = []; | |
//get all of keys of images in S3 movie folder | |
s3.listObjects(params, function (err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else { | |
//foreach list key names to get signed Url | |
data.Contents.forEach(function (obj) { | |
signedUrlParams.Key = obj.Key; | |
var url = s3.getSignedUrl("getObject", signedUrlParams); | |
arrImageUrl.push(url); | |
}) | |
callback(null, arrImageUrl);//return list signedUrl | |
}}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment