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!