Last active
December 13, 2018 20:29
-
-
Save adrianhall/699f4b823f1f3b0b937661f803f3facf to your computer and use it in GitHub Desktop.
This file contains 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
const AWS = require('aws-sdk'); | |
const process = require('process'); | |
AWS.config.update({ | |
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | |
sessionToken: process.env.AWS_SESSION_TOKEN, | |
region: process.env.AWS_REGION | |
}); | |
const s3 = new AWS.S3(); | |
const expiryTime = 3600; // 1 hour = 3600 secomds | |
exports.profilePictureResolver = (event, context, callback) => { | |
const params = { | |
Bucket: process.env.S3_BUCKET.split(':')[5], | |
Key: `profilePictures/${event.source.id}.png`, | |
Expires: expiryTime | |
}; | |
/* | |
** Pre-sign a getObject synchronously | |
*/ | |
const response = { | |
bucket: params.Bucket, | |
key: params.Key, | |
region: process.env.AWS_REGION, | |
url: `${process.env.S3_URL}/profilePictures/${event.source.id}.png` | |
}; | |
/* | |
** Pre-sign a putObject synchronously | |
*/ | |
if (event.sourcce.id === event.identity.username) { | |
response.uploadUrl = s3.getSignedUrl('putObject', params); | |
} | |
callback(null, response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment