All that I found, it's this method GET Bucket But I can't understand how can I get only a list of folders in the current folder. Which prefix and delimiter I need to use? Is that possible at all?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Using
prefix
ofthe/path/to/read/
(note that there is no leading slash, but there is a trailing slash), anddelimiter
of/
, you'll find all the folders within that folder inside<CommonPrefixes>
.Adding a delimiter of '/' to my parameters did the trick for me.
In case anyone needs a NodeJS solution this is what I used:
What Anthony is missing here is that a folder doesn't necessarily have a key associated with it. If a file is created in S3, and given a key like "folder/name.ext", S3 will display a "folder" folder, but it doesn't have a key, meaning you're not getting it in your results.
The only way to catch these folders is to look at the keys themselves, and regex the key name for the "/" character. If I knew C# a little better, I'd write you a code sample, but for reference here's a python example I wrote on another question.
Alternatively another simpler approach is using https://github.com/minio/minio-dotnet
Minio .Net implements minimal API's to work with Amazon S3 and other compatible storage solutions.
Following example shows how you can filter out only directories. Here the CommonPrefix is abstracted as a folder through the ListObjects() API.
For the sake of example, assume I have a bucket in the
USEast1
region calledMyBucketName
, with the following keys:Working with folders can be confusing because S3 does not natively support a hierarchy structure -- rather, these are simply keys like any other S3 object. Folders are simply an abstraction available in the S3 web console to make it easier to navigate a bucket. So when we're working programatically, we want to find keys matching the dimensions of a 'folder' (delimiter '/', size = 0) because they will likely be 'folders' as presented to us by the S3 console.
Note for both examples: I'm using the AWSSDK.S3 version 3.1 NuGet package.
Example 1: All folders in a bucket
This code is modified from this basic example in the S3 documentation to list all keys in a bucket. The example below will identify all keys that end with the delimiter character
/
, and are also empty.Expected output to console:
Example 2: Folders matching a specified prefix
You could further limit this to only retrieve folders matching a specified
Prefix
by setting thePrefix
property on ListObjectsRequest.When applied to Example 1, we would expect the following output:
Further reading: