简体   繁体   中英

Get list of files from an S3 Bucket but not recursively

I would like to list all files in a directory of a S3 bucket but I dont want to include the files that are in subdirectories.

For instance,

example/1.sql
example/2.sql
example/another/1000.sql
example/another/2000.sql

The expected result I want is ["example/1.sql", "example/2.sql"]

Now, I'm getting all the files.

Here is the C# code i'm using:

public async Task<List<string>> ListarArchivos(string subDirectory)
    {
        List<string> archivos = new List<string>();
        ListObjectsV2Request request = new ListObjectsV2Request
        {
            BucketName = bucket,
            Prefix = subDirectory,
            MaxKeys = 100
        };
        ListObjectsV2Response response;
        do
        {
            response = await _clienteAmazonS3.ListObjectsV2Async(request);

            foreach (S3Object entry in response.S3Objects)
            {
                if (entry.Size > 0)
                {
                    archivos.Add(entry.Key);
                }
            }
            request.ContinuationToken = response.NextContinuationToken;
        } while (response.IsTruncated);
        return archivos;
    }

Thanks in advance!

Specifying Delimiter='/' will only return objects that are in the Prefix, excluding subdirectories.

To see an example, use the AWS CLI command:

 aws s3api list-objects --bucket BUCKETNAME --prefix foo/ --delimiter '/'

You will see that it does not return objects in subdirectories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM