如果您使用的是异步版本的 AWS SDK for .NET S3 ListObjectsV2Async,返回空对象列表可能是由于以下原因引起的:
桶名称或前缀不正确,导致返回的对象列表为空。
您没有足够的权限来列出存储桶的对象。
您可以使用以下代码修改您的 AWSSDK S3 ListObjectsV2Async 实现,以确保返回的对象列表正确:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
namespace S3ListAsync
{
class Program
{
static async Task Main()
{
// Replace accessKeyId, secretAccessKey and region with your own values.
var credentials = new Amazon.Runtime.BasicAWSCredentials("accessKeyId", "secretAccessKey");
var config = new AmazonS3Config { RegionEndpoint = RegionEndpoint.USWest2 };
var client = new AmazonS3Client(credentials, config);
// Replace bucketName and prefix with your own values.
var request = new ListObjectsV2Request
{
BucketName = "bucketName",
Prefix = "prefix"
};
ListObjectsV2Response response;
var objects = new List();
do
{
response = await client.ListObjectsV2Async(request);
objects.AddRange(response.S3Objects);
request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
if (objects.Count > 0)
{
Console.WriteLine("Objects:");
foreach (var s3Object in objects)
{
Console.WriteLine(s3Object.Key);
}
}
else
{
Console.WriteLine("No objects found.");
}
}
}
}
上面的代码使用 AWS SDK for .NET Aync 的方式列出指定存储桶中的对象列表。确保替换所有示例代码中的占位符,例如“accessKeyId”、“secretAccessKey”、“region”、“bucketName”和“prefix”。如果您的代码仍然返回空