0

I am trying to list objects using the AWS sdk for Dotnet. https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html

I have:

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();
      }
    }
  }
}

This example is from their docs at https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingNetSDK.html

The error: Exception: System.NullReferenceException: Object reference not set to an instance of an object.

The line the error points to is response = await client.ListObjectsV2Async(request);

I don't know much about Dotnet but I have declared my response the same as in the docs. Thanks!

user_78361084
  • 3,538
  • 22
  • 85
  • 147

1 Answers1

1

It looks to me that you are assigning to local client variable; so your client in ListingObjectsAsync is null. In main() should be (there is no need for config, and the example at your link doesn't mention it)

client = new AmazonS3Client(accessKey, secretKey);

Also, there is no need for RegionEndpoint. Here is your code with some modifications that works for me:

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);
    }
}
Felix
  • 9,248
  • 10
  • 57
  • 89