-
Notifications
You must be signed in to change notification settings - Fork 15
/
s3.js
35 lines (31 loc) · 765 Bytes
/
s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const AWS = require('aws-sdk')
const uuid = require('uuid/v4')
const config = require('./config')
AWS.config.loadFromPath('./s3Config.json')
const s3 = new AWS.S3()
/**
* Upload an image to s3
* @param {string} bucket
* @returns {(contents: AWS.S3.Body) => Promise<string>}
*/
const uploadImage = (bucket, contentType) => async (contents) => {
return new Promise((resolve, reject) => {
const filename = uuid()
s3.putObject({
Bucket: bucket,
Key: filename,
Body: contents,
ContentType: contentType,
ACL: 'public-read'
}, (err, data) => {
if (err) {
reject(err)
} else {
resolve(`https://${bucket}.s3.amazonaws.com/${filename}`)
}
})
})
}
module.exports = {
uploadImage,
}