我试图通过使用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;
}
我传入了我得到的对象的密钥,例如0BE1137F0F3E4703A0F0689346B49871_0.jpg。
但是,这是我得到的响应URL。它没有将对象的密钥附加到响应中,而仅将签名标头附加到响应中。
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>
如果我将此链接粘贴到浏览器中,则表明它是一个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,以显示在浏览器中或作为直接下载链接(使用content-disposition,但也不起作用)代替存储桶的密钥列表。谢谢!
答案 0 :(得分:1)
检查您的 keyString 变量。在发送请求之前将其打印出来,以确保它不为空。单个对象的获取预签名url与列出所有对象的获取预签名url之间的唯一区别是作为参数发送的 Key 。