AWS S3 预签名 URL 改为返回 ListBucketResult

AWS S3 Presigned URL Get returning ListBucketResult instead

我正在尝试使用 GetPreSignedUrlRequest 方法获得预签名 URL 以访问我的私人存储桶中的图像。

我的代码如下:

public string GetPresignedImageURL(string keyString)
{
    string urlString = "";
    try
    {
        GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
        {
            BucketName = bucket,
            Key = keyString,
            Expires = DateTime.Now.AddMinutes(5)
        };
        urlString = _client.GetPreSignedURL(request);
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
     }
     return urlString;
}

我传入了 object 的密钥,例如0BE1137F0F3E4703A0F0689346B49871_0.jpg.

但是,这是我得到的回复 URL。它没有将 object 的密钥附加到响应中,只有签名 headers.

https://<bucket>.ap-southeast-1.amazonaws.com/?X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credential>/20190701/ap-southeast-1/s3/aws4_request&X-Amz-Date=20190701T065534Z&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature>

如果我将这个 link 粘贴到浏览器中,它会显示这是一个 ListBucketResult 请求。

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Name>bucket</Name>
    <Prefix/>
    <Marker/>
    <MaxKeys>1000</MaxKeys>
    <IsTruncated>false</IsTruncated>
<Contents>
    <Key>0BE1137F0F3E4703A0F0689346B49871_0.jpg</Key>
    <LastModified>2019-07-01T06:52:17.000Z</LastModified>
    <ETag>"89db9b468ba0eb45600ed9603fe9f41d"</ETag>
    <Size>1621409</Size>
    <Owner>...</Owner>
    <StorageClass>STANDARD</StorageClass>
</Contents>
<Contents>
    <Key>18F6F2B700A747F983DB26EBC8F3E92F_0.jpg</Key>
    <LastModified>2019-06-28T08:44:40.000Z</LastModified>
    <ETag>"61aa2a6270ec840b185331646ee884a2"</ETag>
    <Size>88703</Size>
    <Owner>...</Owner>
    <StorageClass>STANDARD</StorageClass>
    </Contents>
</ListBucketResult>

我不确定这是不是一个错误,或者我是否遗漏了什么。我想知道如何让图像的预签名 URL 显示在浏览器中或作为直接下载 link(使用 content-disposition,这也不起作用)而不是存储桶的键列表。谢谢!

检查您的 keyString 变量。在发送请求之前将其打印出来,以确保它不为空。获取单个对象的预签名 url 与列出所有对象的预签名 url 之间的唯一区别是您作为参数发送的 Key

在此处查看更多信息:https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/T_Amazon_S3_Model_GetPreSignedUrlRequest.htm