Skip to content

Instantly share code, notes, and snippets.

@baodinh
Created January 30, 2017 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baodinh/eda94e62a34b9049244a80e55b337ef3 to your computer and use it in GitHub Desktop.
Save baodinh/eda94e62a34b9049244a80e55b337ef3 to your computer and use it in GitHub Desktop.
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