Upload files to a specific folder in the bucket with AWS SDK

22,376

Solution 1

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!

Solution 2

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");

Solution 3

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

Solution 4

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

Share:
22,376
Evanss
Author by

Evanss

Updated on June 12, 2020

Comments

  • Evanss
    Evanss almost 4 years

    I'm uploading images to AWS S3 with the developer toolkit for node:

    const aws = require('aws-sdk');
    
    const s3Bucket = process.env.S3_BUCKET;
        const s3 = new aws.S3({
          signatureVersion: 'v4',
          region: process.env.S3_REGION,
          accessKeyId: process.env.S3_ACCESS_KEY_ID,
          secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
        });
    
        const s3Params = {
          Bucket: s3Bucket,
          Key: filename,
          Expires: 60,
          ContentType: filetype,
          ACL: 'public-read',
        };
    

    This works but how can I make image be uploaded to a specific folder in the bucket?