gpt4 book ai didi

c# - 使用 .net SDK 从 Amazon S3 存储桶下载文件夹

转载 作者:行者123 更新时间:2023-11-30 12:20:00 30 4
gpt4 key购买 nike

如何使用 .net sdk 下载 s3 存储桶中存在的整个文件夹。尝试使用以下代码,它会抛出无效 key 。我需要下载存在于存储桶中的嵌套 pesudo 文件夹中的所有文件,并将文件下载限制删除到 1000,这是默认。

public static void DownloadFile()
{
var client = new AmazonS3Client(keyId, keySecret, bucketRegion);
ListObjectsV2Request request = new ListObjectsV2Request
{
BucketName = bucketName + "/private/TargetFolder",
MaxKeys = 1000
};
try
{
ListObjectsV2Response bucketResponse = client.ListObjectsV2(request);
foreach (S3Object o in bucketResponse.S3Objects)
{
var getRequest = new GetObjectRequest
{
BucketName = bucketResponse.Name + "/private/TargetFolder",
Key = bucketResponse.Name +"/private/TargetFolder/"+ o.Key
};
var response = client.GetObject(getRequest);
response.WriteResponseStreamToFile(downloadLocation + "\\" + o.Key);
var responseCode = response.HttpStatusCode;
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"Success downloaded : {o.Key}");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.RequestTimeout)
{
Console.WriteLine("Request Timeout error.");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.ServiceUnavailable)
{
Console.WriteLine("Service Unavailable.");
}
else if (response.HttpStatusCode == System.Net.HttpStatusCode.InternalServerError)
{
Console.WriteLine("Internal Server error.");
}
else
{
Console.WriteLine("Please check the provided AWS Credentials.");
}
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Console.WriteLine(amazonS3Exception.Message);
}

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}

提前致谢!

最佳答案

如果您总是在 S3Objects.Count 中得到 0,请尝试不使用 Delimiter 属性:

public async Task DownloadDirectoryAsync()
{
var bucketRegion = RegionEndpoint.USEast2;
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var client = new AmazonS3Client(credentials, bucketRegion);

var bucketName = "bucketName";
var request = new ListObjectsV2Request
{
BucketName = bucketName,
Prefix = "directorey/",
MaxKeys = 1000
};

var response = await client.ListObjectsV2Async(request);
var utility = new TransferUtility(s3Client);
var downloadPath = "c:\\your_folder";
foreach (var obj in response.S3Objects)
{
utility.Download($"{downloadPath}\\{obj.Key}", bucketName, obj.Key);
}
}

当然,您需要 s3:ListBucket 权限

关于c# - 使用 .net SDK 从 Amazon S3 存储桶下载文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718196/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com