1
votes

I am trying to get a presigned URL to access an image in my private bucket, by using the GetPreSignedUrlRequest method.

My code is as follows:

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;
}

I passed in the key of the object I am getting e.g. 0BE1137F0F3E4703A0F0689346B49871_0.jpg.

However, this is the response URL I get. It did not append the object's key to the response, only the signature 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>

If I paste this link into the browser, it shows that it is a ListBucketResult request.

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>

I am not sure if this is a bug, or if I am missing something here. I would like to know how I can get the presigned URL of the image to either display in the browser or as a direct download link (using content-disposition, which didn't work either) instead of the bucket's keys list. Thank you!

1

1 Answers

1
votes

Check your keyString variable. Print it out before sending the request so that you are sure it isn't null. The only difference between the get presigned url for single object vs get presigned url for listing all objects is the Key that you send as a parameter.

See more here: https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/T_Amazon_S3_Model_GetPreSignedUrlRequest.htm