I am using express + multer-s3 to upload files to AWS S3 service.
Using the following code, I was able to upload the files to S3 Bucket but directly in the bucket.
I want them to be uploaded in a folder inside the bucket.
I was not able to find the option to do so.
Here is the code
AWS.config.loadFromPath("path-to-credentials.json");
var s3 = new AWS.S3();
var cloudStorage = multerS3({
s3: s3,
bucket: "sample_bucket_name",
contentType: multerS3.AUTO_CONTENT_TYPE,
metadata: function(request, file, ab_callback) {
ab_callback(null, {fieldname: file.fieldname});
},
key: function(request, file, ab_callback) {
var newFileName = Date.now() + "-" + file.originalname;
ab_callback(null, newFileName);
},
});
var upload = multer({
storage: cloudStorage
});
router.post("/upload", upload.single('myFeildName'), function(request, response) {
var file = request.file;
console.log(request.file);
response.send("aatman is awesome!");
});
To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.
In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.
The "_$folder$" files are placeholders. Apache Hadoop creates these files when you use the -mkdir command to create a folder in an S3 bucket. Hadoop doesn't create the folder until you PUT the first object. If you delete the "_$folder$" files before you PUT at least one object, Hadoop can't create the folder.
S3 doesn't always have folders (see http://docs.aws.amazon.com/AmazonS3/latest/UG/FolderOperations.html). It will simulate folders by adding a strings separated by / to your filename.
e.g.
key: function(request, file, ab_callback) {
var newFileName = Date.now() + "-" + file.originalname;
var fullPath = 'firstpart/secondpart/'+ newFileName;
ab_callback(null, fullPath);
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With