Upload files to a specific folder in the bucket with AWS SDK
you can write key as FULL PATH
const uploadToS3Bucket = (image, filePath) => {
return new Promise((resolve, reject) => {
let s3 = new AWS.S3({
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
region: process.env.S3_REGION,
});
const bucketName = process.env.S3_BUCKET_NAME;
let bucketPath = filePath;
let params = {
Bucket: bucketName,
Key: bucketPath,
Body: image,
};
s3.putObject(params, function (err, data) {
if (err) {
console.log(err);
} else {
resolve();
}
});
});
};
await uploadToS3Bucket(imageFile,"folder1/folder2/image1.png");
Amazon S3 is a flat storage system that does not actually use folders.
Rather, the Key (filename) includes the full path of the object, eg:
folder1/folder2/foo.txt
The Amazon S3 management console does show bucket contents within folders, but they are an artificial construct (called common prefixes) to make it easier for us humans to understand.
So, if you want to upload to a particular folder, just include the full path in the Key and it will work (or, more accurately, pretend to work!).
Fun fact: You can copy to folder that doesn't exist, and the folder will be 'created' for you. Then, if you delete the object, the folder will be 'deleted', because it never actually existed!
AWS S3 does not have the folder structure. it shows like folder inside S3 bucket but that is Key structure. I used the copy command in one of my work. copying file in S3 bucket folder.
aws s3 cp myapp.zip s3://$S3_BUCKET_NAME/FolderName/FileName
Example
aws s3 cp myapp.zip s3://$S3_BUCKET_NAME/MyFolder/MyFileName
No need to create any folder in AWS S3 bucket it will create the specific folder if it does not exists
The above code is copying myapp.zip file inside the folder - MyFolder of S3 bucket with the file name MyFileName
Actually, we have not the folder object in S3 and all about the key name. When you put the folder likes in your key name for example "folder/myfile.txt," you can see that as a folder on the S3 browser.
The similar question: https://serverfault.com/questions/435827/what-is-the-difference-between-buckets-and-folders-in-amazon-s3