且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 AWS SDK for Node.js 在 s3 上创建文件夹或密钥?

更新时间:2023-11-27 09:11:28

S3 不是典型的文件系统.这是一个对象存储.它有桶和对象.桶用于存储对象,对象包括数据(基本上是文件)和元数据(有关文件的信息).与传统文件系统相比,将 S3 存储桶视为驱动器而不是文件夹更为自然.

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.

您无需在 S3 存储桶中预先创建文件夹结构.即使 cars/ford/ 不存在,您也可以简单地放置带有 cars/ford/focus.png 键的对象.

You don't need to pre-create a folder structure in an S3 bucket. You can simply put an object with the key cars/ford/focus.png even if cars/ford/ does not exist.

了解在这种情况下 API 级别发生的情况很有价值:

It's valuable to understand what happens at the API level in this case:

  • putObject 调用将在 cars/ford/focus.png 创建一个对象,但它不会创建任何代表 cars/ 的中间文件夹结构的东西>汽车/福特/.

  • the putObject call will create an object at cars/ford/focus.png but it will not create anything representing the intermediate folder structure of cars/ or cars/ford/.

实际的文件夹结构并不存在,而是在您调用 listObjects,返回 CommonPrefixes 中的文件夹和 Contents 中的文件.

the actual folder structure does not exist, but is implied through delimiter=/ when you call listObjects, returning folders in CommonPrefixes and files in Contents.

您将无法使用 headObject 因为 cars/ford/ 实际上并不存在(它不是一个对象).相反,您有 2 个选项来查看它(逻辑上)是否存在:

you will not be able to test for the ford sub-folder using headObject because cars/ford/ does not actually exist (it is not an object). Instead you have 2 options to see if it (logically) exists:

  1. 使用前缀=cars/ford/ 调用 listObjects 并在 Contents
  2. 中找到它
  3. 调用 listObjects 前缀=cars/, delimiter=/ 并在CommonPrefixes
  1. call listObjects with prefix=cars/ford/ and find it in Contents
  2. call listObjects with prefix=cars/, delimiter=/ and find it in CommonPrefixes

如果您真的愿意,可以创建一个代表文件夹的 S3 对象.例如,AWS S3 控制台会执行此操作.要在名为 mybucket 的存储桶中创建 myfolder,您可以使用 bucket=mybucket、key=myfolder/和大小 0 发出 putObject 调用.注意尾部的正斜杠.

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.

这是一个使用 awscli 创建类似文件夹的对象的示例:

Here's an example of creating a folder-like object using the awscli:

aws s3api put-object --bucket mybucket --key cars/ --content-length 0

在这种情况下:

  • 文件夹实际上是一个大小为零的对象,其键以/结尾.请注意,如果您不使用尾随/,那么您将获得一个大小为零的对象,该对象似乎是一个文件而不是文件夹.

  • the folder is actually a zero-sized object whose key ends in /. Note that if you leave off the trailing / then you will get a zero-sized object that appears to be a file rather than a folder.

您现在可以通过使用 bucket=mybucket 和 key=cars/发出 headObject 调用来测试 mybucket 中是否存在汽车.

you are now able to test for the presence of cars/ in mybucket by issuing a headObject call with bucket=mybucket and key=cars/.

最后,请注意,您的文件夹分隔符可以是您喜欢的任何内容,例如 +,因为它只是键的一部分,实际上并不是文件夹分隔符(没有文件夹).如果你愿意,你可以在 listObjects 调用中改变你的文件夹分隔符.

Finally, note that your folder delimiter can be anything you like, for example +, because it is simply part of the key and is not actually a folder separator (there are no folders). You can vary your folder delimiter from listObjects call to call if you like.