【问题标题】:Get List of AWS S3 Directory Name from .net core application从 .net 核心应用程序获取 AWS S3 目录名称列表
【发布时间】:2019-05-02 17:21:00
【问题描述】:

我只是在我的 .net 核心 mvc 应用程序上使用 aws s3。我只需要输入 s3 的存储桶名称,然后返回此存储桶中的所有目录名称列表,但这个简单的任务我在互联网上的任何地方都找不到。我已经尝试过 AWS 论坛提供的一些解决方案,但问题是这绝对行不通。贝娄我提供了我的控制器代码以及论坛链接。实际上,他们告诉的问题是 Amazon.S3.IOS3DirectoryInfo 命名空间已从 .net 核心中删除,因此我没有按照他们的建议进行操作。任何人都可以修复我的代码,它将给出.net核心应用程序中的存储桶目录列表?

我正在使用两个 nuget 包-

AWSSDK.CoreAWSSDK.S3

Forum Link - Amazon.S3.IO not supported in .Net Core anymore?

控制器:

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;


public IActionResult Media()
{
    string bucketName = "domain33.com";
    AmazonS3Client s3Client = new AmazonS3Client("Access_Key_ID", "Secret_Access_Key", RegionEndpoint.USEast1);

    var getResponse = s3Client.ListBucketsAsync(new GetObjectRequest
    {
        BucketName = bucketName
    });


    var x = getResponse;

    return View();
}

【问题讨论】:

    标签: c# asp.net-mvc amazon-s3 .net-core


    【解决方案1】:

    您可以尝试在IAmazonS3 上使用ListObjectsV2Async 方法来检索基于AWS's example 的存储桶中所有现有对象的列表。他们的代码如下,以防链接失效:

    // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
    
    using Amazon.S3;
    using Amazon.S3.Model;
    using System;
    using System.Threading.Tasks;
    
    namespace Amazon.DocSamples.S3
    {
        class ListObjectsTest
        {
            private const string bucketName = "*** bucket name ***";
            // Specify your bucket region (an example region is shown).
            private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
    
            private static IAmazonS3 client;
    
            public static void Main()
            {
                client = new AmazonS3Client(bucketRegion);
                ListingObjectsAsync().Wait();
            }
    
            static async Task ListingObjectsAsync()
            {
                try
                {
                    ListObjectsV2Request request = new ListObjectsV2Request
                    {
                        BucketName = bucketName,
                        MaxKeys = 10
                    };
                    ListObjectsV2Response response;
                    do
                    {
                        response = await client.ListObjectsV2Async(request);
    
                        // Process the response.
                        foreach (S3Object entry in response.S3Objects)
                        {
                            Console.WriteLine("key = {0} size = {1}",
                                entry.Key, entry.Size);
                        }
                        Console.WriteLine("Next Continuation Token: {0}", response.NextContinuationToken);
                        request.ContinuationToken = response.NextContinuationToken;
                    } while (response.IsTruncated);
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.ToString());
                    Console.ReadKey();
                }
            }
        }
    }
    

    基于该示例,您可以进行进一步处理或将键添加到字符串列表以进行后续处理,而不是像示例代码那样将其写入控制台。例如,您可以将每个键添加到列表中,然后处理该列表以计算不同的“目录”。

    【讨论】:

    • 问题是他们把directory这个词称为key。这就是为什么我无法在互联网上找到这个例子。
    • 啊 - 是的,白话是魔鬼。 S3 != 完全是一个文件系统,但一般来说,key 表示文件路径,而 S3 并没有真正的第一类“文件夹”或“目录”概念,因为 S3 中不能存在空的“文件夹”。我假设您希望在 S3 中找到独特的“文件夹”。
    猜你喜欢
    • 2018-07-09
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2019-01-26
    • 1970-01-01
    • 2014-12-01
    • 2021-10-13
    相关资源
    最近更新 更多