问题描述
我正在使用适用于Node.js的AWS开发工具包在s3上创建文件夹或密钥.我在Google上搜索,但一无所获.有人知道如何使用适用于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/
或cars/ford/
中间文件夹结构的东西.
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:
- 使用前缀=
cars/ford/
调用listObjects
并在Contents
中找到它 - 使用前缀=
cars/
,定界符=/
调用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,可以发出putObject调用,其中bucket = mybucket,key = myfolder/,大小为0.注意尾随斜杠.
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 = myfolder/subfolder/的headObject调用来测试mybucket中myfolder/subfolder/的存在.
you are now able to test for the presence of myfolder/subfolder/ in mybucket by issuing a headObject call with bucket=mybucket and key=myfolder/subfolder/.
最后,请注意您的文件夹定界符可以是您喜欢的任何字符,例如+,因为它只是键的一部分,实际上不是文件夹分隔符(没有文件夹).您可以根据需要在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上创建文件夹或密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!