Can't Find Image In Aws S3 Despite Successful Upload
I am utilising ng-file-upload module
Solution 1:
If you are submitting file through form then following code might help you.
exports.uploadImageToS3Bucket = function(res, file, folder, callback) {
var fs = require('node-fs');
var AWS = require('aws-sdk');
var filename = file.name; // actual filename of file
var path = file.path; //will be put into a temp directory
var mimeType = file.type;
var accessKeyId = config.get('s3BucketCredentials.accessKeyId');
var secretAccessKeyId = config.get('s3BucketCredentials.secretAccessKey');
var bucketName = config.get('s3BucketCredentials.bucket');
var timestamp = new Date().getTime().toString();
var str = '';
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var size = chars.length;
for (var i = 0; i < 5; i++) {
var randomnumber = Math.floor(Math.random() * size);
str = chars[randomnumber] + str;
}
filename = filename.replace(/\s/g, '');
var fileNewName = str + timestamp + "-" + filename;
fs.readFile(path, function(error, file_buffer) {
if (error) {
responses.imageUploadError(res,{});
}
AWS.config.update({accessKeyId: accessKeyId, secretAccessKey: secretAccessKeyId});
var s3bucket = new AWS.S3();
var params = {Bucket: bucketName, Key: folder + '/' + fileNewName, Body: file_buffer, ACL: 'public-read', ContentType: mimeType};
s3bucket.putObject(params, function(err, data) {
if (err) {
console.log(err);
responses.imageUploadError(res,{});
} else {
return callback(fileNewName);
}
});
});
};
But i would suggest you to use base64encoder because you can directly put a object in s3 from your node server and set the content type as the file type/extension. This Base64 approach is preferrred in most of the case as its doesnt need you to send files over http.
Post a Comment for "Can't Find Image In Aws S3 Despite Successful Upload"