Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty folder in AWS S3 with aws sdk (nodejs)

I'm trying to create a folder in aws s3 I succeded in making a folder but not empty.

const Obj = new AWS.S3().putObject({
 
  Key: `/`, //the file you should put inside the folder
  Bucket: `${bucketName}/${req.body.modpack}`, // main bucket/folder I want to create
});

I'm not able to remove the "key" if I do that I recive and erro that force me to add it

like image 230
Gianmarco Avatar asked Jul 12 '20 20:07

Gianmarco


People also ask

Can I create an empty folder in S3?

Creating a folder Instead, upload an empty folder and specify these settings in the upload configuration. Sign 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 create a folder in.

How do I create a folder on my S3 node?

It is possible to create an S3 object that represents a folder, if you really want to. The AWS S3 console does this, for example. To create myfolder in a bucket named mybucket, you can issue a putObject call with bucket=mybucket, key=myfolder/, and size 0. Note the trailing forward slash.

What is prefix in AWS S3?

You can use prefixes to organize the data that you store in Amazon S3 buckets. A prefix is a string of characters at the beginning of the object key name. A prefix can be any length, subject to the maximum length of the object key name (1,024 bytes).

What is a folder in S3?

In Amazon S3, folders are used to group objects and organize files. Unlike a traditional file system, Amazon S3 doesn't use hierarchy to organize its objects and files. Amazon S3 console supports the folder concept only as a means of grouping (and displaying) objects.


2 Answers

In Amazon S3, buckets and objects are the primary resources, and objects are stored in buckets. Amazon S3 has a flat structure instead of a hierarchy like you would see in a file system. However, for the sake of organizational simplicity, the Amazon S3 console supports the folder concept as a means of grouping objects. Amazon S3 does this by using a shared name prefix for objects (that is, objects have names that begin with a common string). Object names are also referred to as key names.

Also. S3 is not your typical file system. It's an object-store. It has buckets and objects. Buckets are used to store objects, and objects comprise data (basically a file) and metadata (information about the file). When compared to a traditional file system, it's more natural to think of an S3 bucket as a drive rather than as a folder.

So in order to create an object or as we term it a folder, you would need to provide a folder name with a trailing "/" like this:

const Obj = new AWS.S3().putObject({
 
  Key: `EmptyFolder/`, // This should create an empty object in which we can store files 
  Bucket: `${bucketName}`,
});

Ref: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html

like image 147
Umair Ahmed Avatar answered Oct 18 '22 04:10

Umair Ahmed


Folders do not exist in Amazon S3.

However, the Amazon S3 management console allows you to 'create' a folder. It does this by creating a zero-length object with a Key (filename) equal to the 'folder' name. This causes the folder to appear when using the console, or when retrieve CommonPrefixes via the S3 API.

In general, you should not have a need to create folders. Simply copy files to their desired destination and it will magically work, eg:

aws s3 cp foo.txt s3://my-bucket/invoices/january/foo.txt

This will cause the invoices and january folders to 'appear'. If all objects in that directory are deleted, then the folders will 'disappear' (because they never existed).

like image 26
John Rotenstein Avatar answered Oct 18 '22 04:10

John Rotenstein