Created
September 24, 2014 15:06
-
-
Save viccherubini/e3107493f078516555f3 to your computer and use it in GitHub Desktop.
AWS SDK base_url Questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Configuration, using AWS SDK 2.6.16 | |
return [ | |
'includes' => ['_aws'], | |
'services' => [ | |
'default_settings' => [ | |
'params' => [ | |
'key' => 'xxx', | |
'secret' => 'yyyy', | |
'base_url' => 'http://files.example.com' | |
] | |
] | |
] | |
]; | |
// Assume $s3Client is constructed with config above. | |
$result = $s3Client->putObject([ | |
'Bucket' => 'files.example.com', | |
'Key' => 'image.jpg', | |
'SourceFile' => '/path/to/image.jpg', | |
'ContentType' => 'image/jpeg', | |
'ACL' => CannedAc::PUBLIC_READ | |
]); | |
// Receive this error: | |
// [curl] 6: Couldn't resolve host 'files.example.com.files.example.com' [url] http://files.example.com.files.example.com/image.jpg | |
// It seems it attempts to create the URL as: scheme://<bucket-name>.<base-url>. | |
// If you have a CNAME that points files.example.com to files.example.com.s3.amazonaws.com this won't work. |
@jeremeamia 1 year later and I'm finding this info useful. I setup cname with aws sdk php using this information here:
The getObjectUrl works fine yet the upload part gives me the exact same issue as described above. I will try your solution now. I wonder if this is caused by me using SDK 2?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah ha. I see what is happening.
Normally, S3 URLs look something like this: https://s3.amazonaws.com/bucket/key (we call this "path-style"). However, under most conditions, the SDK transforms this to be https://bucket.s3.amazonaws.com/key (we call this "virtual-style"). The transformation is done by
Aws\S3\BucketStyleListener
, and this is what is causing your doubled-up "files.example.com.files.example.com".Using cname URLs, like you are trying to do, is not exactly supported by the SDK, but it can be done easily with an event listener.
Here is a code sample that demonstrates how to hook into the Guzzle request cycle and fix the URL before the request is sent.