Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 230
Evanss Avatar asked Jul 06 '18 16:07

Evanss


People also ask

How do I upload a folder to aws bucket?

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.


4 Answers

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!

like image 182
John Rotenstein Avatar answered Oct 13 '22 20:10

John Rotenstein


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

like image 36
Muhammad Numan Avatar answered Oct 13 '22 19:10

Muhammad Numan


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

like image 3
Gaurav Upadhyay Avatar answered Oct 13 '22 20:10

Gaurav Upadhyay


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

like image 1
Reza Mousavi Avatar answered Oct 13 '22 19:10

Reza Mousavi