LP1*_*P13 5 amazon-s3 aws-sdk aws-sdk-net
我正在使用AWSSDK.S3版本3.3.17.2和AWSSDK.Core版本3.3.21.16来上传文件,然后下载相同的文件。如果文件名包含空格( 或),下面的代码将无法下载该文件#
public class AmazonS3
{
public async Task<string> UploadFileAsync(string sourceFile, string s3BucketUrl)
{
AmazonS3Uri s3Uri = new AmazonS3Uri(s3BucketUrl);
using (var s3 = new AmazonS3Client(s3Uri.Region))
{
using (TransferUtility utility = new TransferUtility(s3))
{
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest
{
BucketName = s3Uri.Bucket,
ContentType = "application/pdf",
FilePath = sourceFile,
Key = s3Uri.Key + Path.GetFileName(sourceFile),
};
await utility.UploadAsync(request).ConfigureAwait(false);
}
}
return Path.Combine(s3BucketUrl, Path.GetFileName(sourceFile));
}
public async Task DownloadFileAsync(string destinationFilePath, string s3Url)
{
var s3Uri = new AmazonS3Uri(s3Url);
var s3Client = new AmazonS3Client(s3Uri.Region);
GetObjectRequest getObjectRequest = new GetObjectRequest
{
BucketName = s3Uri.Bucket,
Key = s3Uri.Key
};
// dispose the underline stream when writing to local file system is done
using (var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest).ConfigureAwait(false))
{
await getObjectResponse.WriteResponseStreamToFileAsync(destinationFilePath, false, default(System.Threading.CancellationToken)).ConfigureAwait(false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后出于测试目的,我上传文件并再次下载相同的文件
AmazonS3 s3 = new AmazonS3();
var uploadedFileS3Link = await s3.UploadFileAsync("C:\\temp\\my test file.pdf", @"https://mybucket.s3-us-west-2.amazonaws.com/development/test/");
// get exception at line below
await s3.DownloadFileAsync("C:\\temp\\downloaded file.pdf",uploadedFileS3Link );
Run Code Online (Sandbox Code Playgroud)
我遇到异常
Amazon.S3.AmazonS3Exception:指定的密钥不存在。---> Amazon.Runtime.Internal.HttpErrorResponseException:远程服务器返回错误:(404) Not Found。---> System.Net.WebException:远程服务器返回错误:(404) 未找到。at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 Promise, Boolean requireSynchronization) --- 从先前抛出异常的位置开始的堆栈跟踪结束 --- 在 System. Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在……
为简洁起见,删除了剩余的例外情况
该文件确实存在于存储桶内。事实上,我可以复制并粘贴 s3url (即uploadedFileS3Link变量的值)并通过浏览器下载文件。
(请注意,实际上我正在尝试下载 1000 多个已上传且名称中带有空格的文件。因此,在上传时删除空格不是一个选项)
更新1 我注意到S3浏览器Url对文件名进行编码
我尝试使用编码的文件路径下载文件https://mybucket.s3-us-west-2.amazonaws.com/development/test/my%20test%20file.pdf
,但仍然不起作用
所以最后我发现了问题所在。我正在使用AmazonS3Uri类来解析给定的 S3 url 并获取密钥、存储桶和区域。返回AmazonS3Uri 我的密钥为development/test/my%20test%20file.pdf
因为内部AmazonS3Uri正在使用System.Uri构建 Uri 然后返回,AbsolutePath它返回编码路径作为键(它应该返回本地路径作为键吗?)
我不知道为什么,但AmazonS3Client不喜欢它,如果你传递编码密钥,它会抛出异常。
因此,为了解决这个问题,我使用解码密钥System.Net.WebUtility.UrlDecode(s3Uri.Key)。所以新的下载方法看起来像
public async Task DownloadFileAsync(string destinationFilePath, string s3Url)
{
var s3Uri = new S3UrlParser(s3Url);
var s3Client = new AmazonS3Client(s3Uri.Region);
GetObjectRequest getObjectRequest = new GetObjectRequest
{
BucketName = s3Uri.Bucket,
Key = System.Net.WebUtility.UrlDecode(s3Uri.Key)
};
// dispose the underline stream when writing to local file system is done
using (var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest).ConfigureAwait(false))
{
await getObjectResponse.WriteResponseStreamToFileAsync(destinationFilePath, false, default(System.Threading.CancellationToken)).ConfigureAwait(false);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8118 次 |
| 最近记录: |