问题描述
我正在使用适用于 Node.js 的 AWS 开发工具包在 s3 上创建文件夹或密钥.我在谷歌上搜索,但我一无所获.有人知道如何使用适用于 Node.js 的 AWS 开发工具包在我的存储桶下创建文件夹吗?以及如何检查此文件夹是否已存在于您的存储桶中?
I'm using AWS SDK for Node.js to create a folder or key on s3. I searched on google, but I got nothing. Does anybody know how can I create a folder under my bucket with AWS SDK for Node.js?and how can you check if this folder exists in your bucket already?
如果您使用 console.aws.amazon.com
,您可以轻松地在您的存储桶中创建一个文件夹.似乎我没有弄清楚如何使用适用于 Node.js 的 AWS 开发工具包创建它?
if you use console.aws.amazon.com
, you can create a folder in your bucket easily. it seems I didn't figure it out how to create it with AWS SDK for Node.js?
推荐答案
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 ofcars/
orcars/ford/
.
实际的文件夹结构不存在,但在您调用 ,返回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:
- 使用 prefix=
cars/ford/
调用listObjects
并在Contents
中找到它 - 使用prefix=
cars/
, delimiter=/
调用listObjects
并在CommonPrefixes
中找到它
- call
listObjects
with prefix=cars/ford/
and find it inContents
- call
listObjects
with prefix=cars/
, delimiter=/
and find it inCommonPrefixes
如果您真的愿意,可以创建一个代表文件夹的 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 中cars/的存在.
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.
这篇关于如何使用适用于 Node.js 的 AWS 开发工具包在 s3 上创建文件夹或密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!