Skip to content

Add Node.js 10.x Version Lambda script #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions lambda/GetSignedURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,69 @@ var AWS = require('aws-sdk')
var s3 = new AWS.S3();
var bucketName = process.env.AWS_BUCKET_NAME

exports.handler = async (event) => {
let payload = {};
if(event.body){
payload = JSON.parse(event.body);
}
const params = {
Bucket: bucketName,
Key: payload.filePath,
Expires: 3600,
ContentType: payload.contentType
}
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials": true,
"Access-Control-Allow-Headers": 'content-type',
},
body: {},
}
if (!payload.hasOwnProperty('contentType')) {
response.body = JSON.stringify({
err: 'Missing contentType'
})
return response;
}

if (!payload.hasOwnProperty('filePath')) {
response.body = JSON.stringify({
err: 'Missing filePath'
})
return response;
}

try{
const url = await _getUrl(params);
response.body = JSON.stringify({
url
})
}catch(err){
response.body = JSON.stringify({
err: err
})
}
return response;

function _getUrl(params){
return new Promise((resolve, reject) => {
s3.getSignedUrl('putObject', params, (err, url) => {
if (err) {
reject(err);
} else {
resolve(url);
}
})
});
}
};

var AWS = require('aws-sdk')
var s3 = new AWS.S3();
var bucketName = process.env.AWS_BUCKET_NAME

exports.handler = (event, context) => {
if (!event.hasOwnProperty('contentType')) {
context.fail({ err: 'Missing contentType' })
Expand Down
62 changes: 62 additions & 0 deletions lambda/GetSignedURLv4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var AWS = require('aws-sdk')
var s3 = new AWS.S3();
var bucketName = process.env.AWS_BUCKET_NAME

exports.handler = async (event) => {
let payload = {};
if(event.body){
payload = JSON.parse(event.body);
}
const params = {
Bucket: bucketName,
Key: payload.filePath,
Expires: 3600,
ContentType: payload.contentType
}
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials": true,
"Access-Control-Allow-Headers": 'content-type',
},
body: {},
}
if (!payload.hasOwnProperty('contentType')) {
response.body = JSON.stringify({
err: 'Missing contentType'
})
return response;
}

if (!payload.hasOwnProperty('filePath')) {
response.body = JSON.stringify({
err: 'Missing filePath'
})
return response;
}

try{
const url = await _getUrl(params);
response.body = JSON.stringify({
url
})
}catch(err){
response.body = JSON.stringify({
err: err
})
}
return response;

function _getUrl(params){
return new Promise((resolve, reject) => {
s3.getSignedUrl('putObject', params, (err, url) => {
if (err) {
reject(err);
} else {
resolve(url);
}
})
});
}
};
2 changes: 1 addition & 1 deletion lambda/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Quickstart

1. create a blank AWS Lambda function
2. choose Node 4.3 runtime
2. choose Node 10.x or 4.3 runtime (If using 4.3 runtime, use GetSignedURLv4.js)
3. paste the code in `GetSignedURL.js` to the Lambda function code
4. add an env variable `AWS_BUCKET_NAME` with value equals to your S3 bucket
name
Expand Down