Amazon S3客户端无法下载带空格或哈希的文件?

时间:2018-03-16 16:02:12

标签: amazon-s3 aws-sdk aws-sdk-net

我使用AWSSDK.S3版本3.3.17.2AWSSDK.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:指定的密钥不存在。 ---&GT;   Amazon.Runtime.Internal.HttpErrorResponseException:远程服务器   返回错误:(404)未找到。 ---&GT; System.Net.WebException:The   远程服务器返回错误:(404)未找到。在   System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
  在   System.Threading.Tasks.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func 2 endFunction,Action 1 endAction, Task 1 promise,Boolean   requiresSynchronization)---来自之前的堆栈跟踪结束   抛出异常的位置--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)......

为了简洁,

删除了剩余的异常

该文件确实存在于存储桶中。实际上我可以复制并粘贴s3url(即uploadedFileS3Link变量的值)并通过浏览器下载文件。

(请注意,实际上我正在尝试下载已经上传的1000多个文件,其名称中包含空格。因此在上传时删除空格不是一种选择)

更新1 我注意到S3浏览器Url编码文件名

enter image description here

我尝试使用编码文件路径https://mybucket.s3-us-west-2.amazonaws.com/development/test/my%20test%20file.pdf下载文件  但它仍然无法正常工作

1 个答案:

答案 0 :(得分:1)

所以最后我发现了什么问题。我使用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);
        }
    }