我正在使用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);
}
}
}
然后出于测试目的,我正在上传文件并再次下载相同的文件
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 );
我得到了例外
Amazon.S3.AmazonS3Exception:指定的键不存在。---> Amazon.Runtime.Internal.HttpErrorResponseException:远程服务器返回错误:(404)未找到。---> System.Net.WebException:远程服务器返回错误:(404)未找到。在 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean requiresSynchronization) --- 从先前抛出异常的位置结束堆栈跟踪--- 在 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
但仍然无法正常工作