我正在尝试使用适用于 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 了解不多,但我已经宣布我的回复与文档中的相同。谢谢!