3
votes

I want to create presigned S3 URL as mentioned here: https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html

My code is quite similar to the example mentioned in the url:

$sdk = new Aws\Sdk( [
    'region' => 'eu-west-2',
    'version' => 'latest',
] );

$s3Client = $sdk->createS3();

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'books.com',
    'Key'    => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

// Get the actual presigned-url
$presignedUrl = (string) $request->getUri();

The above generates urls like so:

https://s3.eu-west-2.amazonaws.com/books.com/testKey?X-Amz-Content-Sha256=....

This is as expected. However my S3 bucket has Static Website Hosting and I use a CNAME record allowing me to use a different base url.

Therefore I want the following URL instead:

http://books.com/my-bucket/testKey?X-Amz-Content-Sha256=....

How can I do this?

1

1 Answers

4
votes

You can set the endpoint to your bucket domain name:

$sdk = new Aws\Sdk( [
    'region' => 'eu-west-2',
    'version' => 'latest',
    'endpoint' => 'http://books.com',
    'bucket_endpoint' => true
] );

This will generate a signed URL that looks like this:

http://books.com/testKey?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI2V4Lxxxxxxxxxxx%2F20171116%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20171116T191003Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1200&X-Amz-Signature=0b735cb661b1d2e25c7f5b477d4c657f160a85aa53bee3ea91244340f6d37dee