-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3_upload.js
46 lines (39 loc) · 1.06 KB
/
s3_upload.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
44
45
46
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const fs = require('fs');
// Load the SDK for JavaScript
const AWS = require('aws-sdk');
// Set the region
AWS.config.update({
region: 'us-west-1',
});
const credentials = new AWS.SharedIniFileCredentials({
profile: 'default',
});
AWS.config.credentials = credentials;
// Create S3 service object
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
});
// call S3 to retrieve upload file to specified bucket
const uploadParams = {
Bucket: process.argv[2],
Key: '',
Body: '',
};
const file = process.argv[3];
// Configure the file stream and obtain the upload parameters
const fileStream = fs.createReadStream(file);
fileStream.on('error', err => (
console.log('File Error', err)
));
uploadParams.Body = fileStream;
uploadParams.Key = `review/${path.basename(file)}`;
// call S3 to retrieve upload file to specified bucket
s3.upload(uploadParams, (err, data) => {
if (err) {
console.log('Error', err);
} if (data) {
console.log('Upload Success', data.Location);
}
});