Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. var aws = require('aws-sdk');
  2. var promise = require('bluebird');
  3. var fs = promise.promisifyAll(require('fs'));
  4.  
  5. var awsConfig = {
  6. region: "aws.region",
  7. accessKeyId: "aws.accessKeyId",
  8. secretAccessKey: "aws.secretAccessKey",
  9. sslEnabled: true
  10. };
  11.  
  12. var s3 = promise.promisifyAll(new aws.S3(awsConfig));
  13.  
  14. /**
  15. * @param {Object} data - required s3 properties regarding object
  16. * @param {String} data.path - absolute path of the object,
  17. * @param {String} data.name - name define object relative path, example: path/to/object.ext
  18. * @param {String} data.mimeType - aws require content type, provide
  19. * @param {String} [data.acl] - default permission is private
  20. * @param {String} bucketName - cache-control max age in days
  21. * @param {Number} [maxAge] - cache-control max age in days
  22. * @returns {promise} - checksum
  23. */
  24. function uploadObject(data, bucketName, maxAge){
  25. return fs.readFileAsync(data.path)
  26. .then(function(buffer){
  27. if(data.mimeType){
  28. var params = {};
  29. params.Bucket = bucketName;
  30. params.Body = buffer;
  31. params.Key = data.name;
  32. params.ContentType = data.mimeType;
  33. params.ACL = data.acl || 'private';
  34. params.StorageClass = 'STANDARD';
  35. if(maxAge) params.CacheControl = 'max-age=' + (3600 * 24 * maxAge);
  36. return s3.putObjectAsync(params);
  37. } else
  38. throw new Error('mime type is mandatory');
  39. }).then(function(checksum){
  40. return checksum;
  41. }).catch(function(err){
  42. console.log("Error in CDN file upload, ", data.path, err);
  43. return err;
  44. });
  45.  
  46. }
  47.  
  48.  
  49.  
  50. module.exports = {
  51. uploadObject: uploadObject
  52. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement