admin管理员组

文章数量:1231080

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

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

Share Improve this question asked Jul 12, 2020 at 20:09 GianmarcoGianmarco 8322 gold badges16 silver badges40 bronze badges 2
  • As pointed out in the answer, you cannot create empty "folders" in S3 – Chris Commented Jul 12, 2020 at 20:34
  • There's also no need to create "an empty" folder. Just add the file in whatever path/key you want and you'll find it there once uploaded. – peter n Commented Jul 12, 2020 at 20:52
Add a comment  | 

3 Answers 3

Reset to default 25

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

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).

The only option for empty in s3 is a bucket so once a bucket is created you can add any data inside it in any dynamic folder structure. The folder structure is created as per the path you have set for the file to exist example could be bucket_name/folder_name/another_folder_name/file_name & this folder can be up to N number of depths.

本文标签: javascriptCreate an empty folder in AWS S3 with aws sdk (nodejs)Stack Overflow