fileUploader.js 921 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * fileUploader
  3. */
  4. var aws = require('aws-sdk');
  5. var config = require('config');
  6. module.exports = {
  7. // deleteFile: function(filePath, callback) {
  8. // // TODO 実装する
  9. // },
  10. uploadFile: function(filePath, contentType, fileStream, options, callback) {
  11. var awsConfig = config.aws;
  12. aws.config.update({
  13. accessKeyId: awsConfig.accessKeyId,
  14. secretAccessKey: awsConfig.secretAccessKey,
  15. region: awsConfig.region
  16. });
  17. var s3 = new aws.S3();
  18. var params = {Bucket: awsConfig.bucket};
  19. params.ContentType = contentType;
  20. params.Key = filePath;
  21. params.Body = fileStream;
  22. params.ACL = 'public-read';
  23. s3.putObject(params, function(err, data) {
  24. callback(err, data);
  25. });
  26. },
  27. generateS3FillUrl: function(filePath) {
  28. var awsConfig = config.aws;
  29. var url = 'https://' + awsConfig.bucket +'.s3.amazonaws.com/' + filePath;
  30. return url;
  31. }
  32. };