Guest User

Untitled

a guest
Feb 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. import * as fs from "fs";
  2. import * as AWS from "aws-sdk";
  3.  
  4. const BUCKET_NAME = "<<bucket name>>";
  5. const IAM_USER_KEY = "<<user key>>";
  6. const IAM_USER_SECRET = "<<user secret>>";
  7.  
  8. const s3bucket = new AWS.S3({
  9. accessKeyId: IAM_USER_KEY,
  10. secretAccessKey: IAM_USER_SECRET
  11. });
  12.  
  13. export function uploadToS3(fileName: string): Promise<any> {
  14. const readStream = fs.createReadStream(fileName);
  15.  
  16. const params = {
  17. Bucket: BUCKET_NAME,
  18. Key: "myapp" + "/" + fileName,
  19. Body: readStream
  20. };
  21.  
  22. return new Promise((resolve, reject) => {
  23. s3bucket.upload(params, function(err, data) {
  24. readStream.destroy();
  25.  
  26. if (err) {
  27. return reject(err);
  28. }
  29.  
  30. return resolve(data);
  31. });
  32. });
  33. }
Add Comment
Please, Sign In to add comment