将文件列在特定的“文件夹”中AWS S3存储桶 [英] Listing files in a specific "folder" of a AWS S3 bucket

查看:108
本文介绍了将文件列在特定的“文件夹”中AWS S3存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要列出我的S3存储桶中包含的某个文件夹中包含的所有文件。

I need to list all files contained in a certain folder contained in my S3 bucket.

文件夹结构如下

/my-bucket/users/<user-id>/contacts/<contact-id>

我有与用户和某个用户联系人相关的文件相关的文件。
我需要列出两者。

I have files related to users and files related to a certain user's contact. I need to list both.

列出我正在使用此代码的文件:

To list files I'm using this code:

ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName("my-bucket")
                .withPrefix("some-prefix").withDelimiter("/");
ObjectListing objects = transferManager.getAmazonS3Client().listObjects(listObjectsRequest);

要列出某个用户的文件,我正在使用此前缀:

To list a certain user's files I'm using this prefix:

users /< user-id> /

我正确得到了目录中的所有文件(联系人子目录除外),例如:

and I'm correctly getting all files in the directory excluding contacts subdirectory, for example:

users/<user-id>/file1.txt
users/<user-id>/file2.txt
users/<user-id>/file3.txt

要列出某个用户联系人的文件而不是我使用此前缀:

To list a certain user contact's files instead I'm using this prefix:

用户/< user-id> / contacts /< contact-id> /

但是在这种情况我也将$ b​​ $ b目录本身作为返回对象:

but in this case I'm getting also the directory itself as a returned object:

users/<user-id>/contacts/<contact-id>/file1.txt
users/<user-id>/contacts/<contact-id>/file2.txt
users/<user-id>/contacts/<contact-id>/

为什么我会出现这种行为?这两个上市要求之间有什么不同?我只需列出目录中的文件,不包括子目录。

Why am I getting this behaviour? What's different beetween the two listing requests? I need to list only files in the directory, excluding sub-directories.

推荐答案

S3中的所有内容都是一个对象。对你来说,它可能是文件和文件夹。但对于S3,它们只是对象。

Everything in S3 is an object. To you, it may be files and folders. But to S3, they're just objects.

以分隔符结尾的对象(在大多数情况下为 / )通常被认为是一个文件夹,但情况并非总是如此。这取决于应用程序。同样,在您的情况下,您将其解释为文件夹。 S3不是。这只是另一个对象。

Objects that end with the delimiter (/ in most cases) are usually perceived as a folder, but it's not always the case. It depends on the application. Again, in your case, you're interpretting it as a folder. S3 is not. It's just another object.

在上面的例子中,对象 users /< user-id> / contacts /< contact-id> / 在S3中作为不同的对象存在,但对象 users /< user-id> / 则不存在。这是你的回答中的差异。为什么他们是这样的,我们不能告诉你,但有人在一个案件中制造了这个对象,而在另一个案件中则没有。您没有在AWS管理控制台中看到它,因为控制台将其解释为文件夹并将其隐藏起来。

In your case above, the object users/<user-id>/contacts/<contact-id>/ exists in S3 as a distinct object, but the object users/<user-id>/ does not. That's the difference in your responses. Why they're like that, we cannot tell you, but someone made the object in one case, and didn't in the other. You don't see it in the AWS Management Console because the console is interpreting it as a folder and hiding it from you.

由于S3只是将这些内容视为对象,它不会为你排除某些东西。由客户来处理应该处理的对象。

Since S3 just sees these things as objects, it won't "exclude" certain things for you. It's up to the client to deal with the objects as they should be dealt with.

您的解决方案

由于你是不想要文件夹对象的人,你可以通过检查 / 的最后一个字符来自己排除它。如果是,则忽略响应中的对象。

Since you're the one that doesn't want the folder objects, you can exclude it yourself by checking the last character for a /. If it is, then ignore the object from the response.

这篇关于将文件列在特定的“文件夹”中AWS S3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆