Last active
February 15, 2019 05:17
-
-
Save masnun/3953dc0213c1fd601466b2b3abbed0ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as fs from "fs"; | |
import * as AWS from "aws-sdk"; | |
const BUCKET_NAME = "<<bucket name>>"; | |
const IAM_USER_KEY = "<<user key>>"; | |
const IAM_USER_SECRET = "<<user secret>>"; | |
const s3bucket = new AWS.S3({ | |
accessKeyId: IAM_USER_KEY, | |
secretAccessKey: IAM_USER_SECRET | |
}); | |
export function uploadToS3(fileName: string): Promise<any> { | |
const readStream = fs.createReadStream(fileName); | |
const params = { | |
Bucket: BUCKET_NAME, | |
Key: "myapp" + "/" + fileName, | |
Body: readStream | |
}; | |
return new Promise((resolve, reject) => { | |
s3bucket.upload(params, function(err, data) { | |
readStream.destroy(); | |
if (err) { | |
return reject(err); | |
} | |
return resolve(data); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment