Last active
January 3, 2020 21:28
-
-
Save bertolo1988/111d3137ee994c10f88e51979c4020b8 to your computer and use it in GitHub Desktop.
Connect and upload a picture into s3
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
var AWS = require('aws-sdk') | |
const fs = require('fs') | |
const BUCKET = 'bucket' | |
const REGION = 'eu-west-1' | |
const ACCESS_KEY = 'AWS_ACCESS_KEY' | |
const SECRET_KEY = 'AWS_SECRET_KEY' | |
const localImage = './cat.png' | |
const imageRemoteName = `catImage_${new Date().getTime()}.png` | |
AWS.config.update({ | |
accessKeyId: ACCESS_KEY, | |
secretAccessKey: SECRET_KEY, | |
region: REGION | |
}) | |
var s3 = new AWS.S3() | |
s3.putObject({ | |
Bucket: BUCKET, | |
Body: fs.readFileSync(localImage), | |
Key: imageRemoteName | |
}) | |
.promise() | |
.then(response => { | |
console.log(`done! - `, response) | |
console.log( | |
`The URL is ${s3.getSignedUrl('getObject', { Bucket: BUCKET, Key: imageRemoteName })}` | |
) | |
}) | |
.catch(err => { | |
console.log('failed:', err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment