0

我正在尝试使用适用于 Dotnet 的 AWS sdk 列出对象。https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html

我有:

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Amazon.DocSamples.S3
{
    class ListObjectsTest
  {
    private const string bucketName = "mybucket";
    private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest1;
    private static IAmazonS3 client;
    public static void Main()
    {
      string accessKey = "mykey";
      string secretKey = "xxxxx";
      var config = new AmazonS3Config { 
        ServiceURL = "http://example.com",
      };

      AmazonS3Client client = new AmazonS3Client(
        accessKey,
        secretKey,
        config
      );
      ListingObjectsAsync().Wait();
    }

    static async Task ListingObjectsAsync()
    {
      try
      {
        ListObjectsV2Request request = new ListObjectsV2Request
        {
          BucketName = bucketName,
          MaxKeys = 10
        };
        ListObjectsV2Response response;
        do
        {
          response = await client.ListObjectsV2Async(request);
          Console.WriteLine(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();
      }
    }
  }
}

此示例来自他们位于https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html的文档

错误:Exception: System.NullReferenceException: Object reference not set to an instance of an object.

错误指向的行是response = await client.ListObjectsV2Async(request);

我对 Dotnet 了解不多,但我已经宣布我的回复与文档中的相同。谢谢!

4

1 回答 1

1

在我看来,您正在分配给局部client变量;所以你的clientinListingObjectsAsync是空的。main()应该是(不需要配置,链接中的示例没有提及)

client = new AmazonS3Client(accessKey, secretKey);

此外,不需要 RegionEndpoint。这是您的代码,并进行了一些适合我的修改:

private const string bucketName = "mybucket";
private static IAmazonS3 _client;

public static async Task Main()
{
    const string accessKey = "accesskey";
    const string secretKey = "secretkey";

    _client = new AmazonS3Client(accessKey, secretKey);
    await ListingObjectsAsync();
}

private static async Task ListingObjectsAsync()
{
    try
    {
        ListObjectsV2Request request = new ListObjectsV2Request
        {
            BucketName = bucketName,
            MaxKeys = 10
        };
        ListObjectsV2Response response;
        do
        {
            response = await _client.ListObjectsV2Async(request);
            Console.WriteLine(response);
            foreach (S3Object entry in response.S3Objects)
            {
                Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size);
            }
            Console.WriteLine($"Next Continuation Token: {response.NextContinuationToken}");
            request.ContinuationToken = response.NextContinuationToken;
        } while (response.IsTruncated);
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception);
        Console.ReadKey();
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception: " + e);
    }
}
于 2020-06-29T01:13:09.690 回答