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

查看:25
本文介绍了列出特定“文件夹"中的文件一个 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//

并且我正确地获取了目录中不包括 contacts 子目录的所有文件,例如:

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:

users//contacts//

但在这种情况下,我也得到了目录本身作为返回的对象:

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//contacts// 作为一个不同的对象存在于 S3 中,但对象 users// 没有.这就是你的反应不同.为什么他们会这样,我们不能告诉你,但有人在一个案例中制造了这个物体,而在另一个案例中没有.您在 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天全站免登陆