-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathaws.js
43 lines (35 loc) · 1.19 KB
/
aws.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
36
37
38
39
40
41
42
43
const fs = require('fs')
const AWS = require('aws-sdk')
const settings = require('../config')
AWS.config.update(settings.aws)
const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
async function upload (pathToFile, fileName) {
return new Promise((resolve, reject) => {
const uploadParams = { Bucket: settings.aws.iconBucket, Key: '', Body: '' }
if (/\.svg$/i.test(pathToFile)) {
uploadParams.ContentType = 'image/svg+xml'
} else if (/\.png$/i.test(pathToFile)) {
uploadParams.ContentType = 'image/png'
} else if (/\.jpg/i.test(pathToFile)) {
uploadParams.ContentType = 'image/jpeg'
} else if (/\.gif/i.test(pathToFile)) {
uploadParams.ContentType = 'image/gif'
}
const fileStream = fs.createReadStream(pathToFile)
fileStream.on('error', function (err) {
reject(err)
})
uploadParams.Body = fileStream
uploadParams.Key = fileName
s3.upload(uploadParams, function (err, data) {
if (err) {
reject(err)
} if (data) {
resolve(data.Location)
}
})
})
}
module.exports = {
upload
}