C#列出amazon S3文件夹下所有文件名的文件

时间:2016-08-30 10:32:35

标签: c# amazon-web-services amazon-s3

使用C#和amazon .Net SDK,能够列出亚马逊S3文件夹中的所有文件,如下所示:

ListObjectsRequest request = new ListObjectsRequest();           
            request.BucketName = _bucketName; //Amazon Bucket Name
            request.Prefix = _sourceKey; //Amazon S3 Folder path           
            do
            {
                ListObjectsResponse response = _client.ListObjects(request);//_client - AmazonS3Client

Out put:

    Folder
    Folder/file1.pdf
    Folder/file2.pdf
    Folder/file3.pdf

但是我想要实现这样的目标: 期望的输出:

file1.pdf
file2.pdf
file3.pdf

提前致谢

5 个答案:

答案 0 :(得分:12)

您还可以使用以下c#代码检索文件信息。

    string bucketName = "your bucket";
    AmazonS3Client s3Client = new AmazonS3Client("your access key", "your secret key", cfg);
    S3DirectoryInfo dir = new S3DirectoryInfo(s3Client, bucketName, "your AmazonS3 folder name");
    foreach (IS3FileSystemInfo file in dir.GetFileSystemInfos())
    {
        Console.WriteLine(file.Name);
        Console.WriteLine(file.Extension);
        Console.WriteLine(file.LastWriteTime);
    }

我正在使用amazon AWSSDK.Core和AWSSDK.S3版本3.1.0.0 for .net 3.5。我希望它可以帮到你

答案 1 :(得分:3)

    AmazonS3Client client = new AmazonS3Client();
    ListObjectsRequest listRequest = new ListObjectsRequest
    {
        BucketName = "your-bucket-name",
        Prefix = "example/path"
    };

    ListObjectsResponse listResponse;
    do
    {
        listResponse = client.ListObjects(listRequest);
        foreach (S3Object obj in listResponse.S3Objects)
        {
            Console.WriteLine(obj.Key);
            Console.WriteLine(" Size - " + obj.Size);
        }

        listRequest.Marker = listResponse.NextMarker;
    } while (listResponse.IsTruncated);

https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/M_Amazon_S3_AmazonS3_ListObjects.htm

答案 2 :(得分:0)

string bucketName = your bucket name;
S3DirectoryInfo dir = new S3DirectoryInfo(client, bucketName, "folder name");
foreach (IS3FileSystemInfo file in dir.GetFiles())
{
    Console.WriteLine(file.Name);
    Console.WriteLine(file.Extension);
    Console.WriteLine(file.LastWriteTime);
}

答案 3 :(得分:0)

        AmazonS3Client s3Client = new AmazonS3Client(S3_ACCESS_KEY, S3_SECRET_ACCESS_KEY, S3_REGIAO);
        var lista = s3Client.ListObjectsAsync(S3_BUCKET, $"{S3_SUBDIRETORIO}/{produto}/{produto}.").Result;
        var files = lista.S3Objects.Select(x => x.Key);
        var arquivos = files.Select(x => Path.GetFileName(x)).ToList();

答案 4 :(得分:0)

让我们讲清楚,简单并且有一些说明。
众所周知,在S3中没有目录(文件夹)的概念。啊什么 所以S3里面的所有东西不过是对象。
让我们考虑以下示例s3存储桶-存储桶名称为 testBucket ,目录名称为 testDirectory ,该目录包含两个文件 testImage.jpg testUserData.txt

  • testBucket
    • testDirectory
      • testImage.jpg
      • testUserData.txt
AmazonS3Client s3Client = new AmazonS3Client();
string bucketName = "testBucket";
// to list out all the elements inside of a director [To know more][1]
string prefix = "testDirectory/";

ListObjectsRequest request = new ListObjectsRequest
                {
                    BucketName = bucketName,
                    Prefix = prefix
                };
ListObjectsResponse response =  s3Client.ListObjects(request);

foreach (S3Object itemsInsideDirectory in response .S3Objects)
                {
                    Console.WriteLine(itemsInsideDirectory.Key);
                }

输出:
testDirectory /
testDirectory / testImage.jpg
testDirectory / testUserData.txt