简体   繁体   English

使用 aws sdk (nodejs) 在 AWS S3 中创建一个空文件夹

[英]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 .我正在尝试在 aws s3创建一个文件夹,我成功创建了一个文件夹但不是的。

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如果我这样做,我将无法删除密钥”,并且错误地迫使我添加它

In Amazon S3, buckets and objects are the primary resources, and objects are stored in buckets.在 Amazon S3 中,存储桶和对象是主要资源,对象存储在存储桶中。 Amazon S3 has a flat structure instead of a hierarchy like you would see in a file system. Amazon S3 具有平面结构,而不是您在文件系统中看到的层次结构。 However, for the sake of organizational simplicity, the Amazon S3 console supports the folder concept as a means of grouping objects.但是,为了组织简单,Amazon S3 控制台支持将文件夹概念作为对对象进行分组的一种方式。 Amazon S3 does this by using a shared name prefix for objects (that is, objects have names that begin with a common string). Amazon S3 通过为对象使用共享名称前缀(即,对象的名称以通用字符串开头)来实现这一点。 Object names are also referred to as key names. Object 名称也称为键名。

Also.还。 S3 is not your typical file system. S3 不是典型的文件系统。 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.与传统文件系统相比,将 S3 存储桶视为驱动器而不是文件夹更为自然。

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:因此,为了创建 object 或者我们称之为文件夹,您需要提供一个带有尾随“/”的文件夹名称,如下所示:

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参考: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html

Folders do not exist in Amazon S3. Amazon S3 中不存在文件夹。

However, the Amazon S3 management console allows you to 'create' a folder.但是,Amazon S3 管理控制台允许您“创建”一个文件夹。 It does this by creating a zero-length object with a Key (filename) equal to the 'folder' name.它通过创建一个长度为零的 object 来实现这一点,其密钥(文件名)等于“文件夹”名称。 This causes the folder to appear when using the console, or when retrieve CommonPrefixes via the S3 API.这会导致在使用控制台或通过 S3 API 检索CommonPrefixes时出现该文件夹。

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'.这将导致invoicesjanuary文件夹“出现”。 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. s3 中空的唯一选项是存储桶,因此一旦创建了存储桶,您就可以在任何动态文件夹结构中添加任何数据。 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.文件夹结构是根据您为要存在的文件设置的路径创建的,示例可以是 bucket_name/folder_name/another_folder_name/file_name 并且该文件夹最多可以有 N 个深度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM