首先是在资源管理器上添加引用,管理Nuget下载Amazon提供的AWS.S3的SDK
添加如下引用
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
然后声明需要的变量
首先是你存在S3文件的的桶名
然后是区域这个也是在AWS控制台上可以看到
下面三个就是验证参数,这个根据不同的权限需求用的不一样
再下面是一个awss3的客户端
const string bucketName = "";
static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
static string accessKey = "";
static string secrectKey = "";
static string sessionToken = "";
static IAmazonS3 client;
这里是主函数,首先就是把客户端对象实例化出来。然后进入到一个异步的获取对象列表的任务中
public static void Main()
{
client = new AmazonS3Client(accessKey, secrectKey, sessionToken, bucketRegion);
ListingObjectsAsync().Wait();
}
首先实例化一个需求对象用作入参,字段是桶名和文件路径
static async Task ListingObjectsAsync()
{
try
{
ListObjectsV2Request request = new ListObjectsV2Request
{
BucketName = bucketName,
Prefix= "devices-pic/Camera"
};
ListObjectsV2Response response;
do
{
response = await client.ListObjectsV2Async(request);
// Process the 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();
}
}