Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set S3 vhost? #347

Closed
dmyers opened this issue Sep 6, 2014 · 9 comments
Closed

How to set S3 vhost? #347

dmyers opened this issue Sep 6, 2014 · 9 comments

Comments

@dmyers
Copy link

dmyers commented Sep 6, 2014

In a previous PHP SDK for S3 there was a way to set the virtual host so if you had a CNAME of s3.yourdomain.com and it was pointed to s3.amazonaws.com. you could set the vhost option and it getObjectUrl calls would use the correct URL.

It appears to have worked by setting region to null and setting the base_url to say http://s3.yourdomain.com, but I'm having trouble getting that to work either.

http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html

Do you know how to set this in the current SDK?

@mtdowling
Copy link
Member

You should be able to create a client that has a custom base_url and a region set to the appropriate region in which the CNAME bucket points to (e.g., if you created a bucket in us-west-1, then your region should be us-west-1).

I'm having trouble getting that to work either.

How is it failing?

@dmyers
Copy link
Author

dmyers commented Sep 6, 2014

I'm passing the 'base_url' to the S3Client::factory in my config is that the wrong way to set it?

Trying to set the key to test, and the base_url to http://s3.yourdomain.com without a bucket I get:

[curl] 6: Could not resolve host: test [url] http://test

@mtdowling
Copy link
Member

What did you pass as the base_url? When provided a full URL (e.g., https://foo.mysite.com), you should be able to create presigned URLs.

Trying to set the key to test, and the base_url to http://s3.yourdomain.com without a bucket I get:

Can you show your code? What operation are you sending?

@kara-todd
Copy link

I'm also having a bit of trouble with this.

I've set the base_url parameter like so:

 'base_url' => 'https://s3.amazonaws.com/my-bucket/',
 'key'      => $key,
 'secret'   => $secret,

However, when I call $s3Client->getObjectUrl('my-bucket', 'file.txt')
I always get back https://my-bucket.s3.amazonaws.com/file.txt

Am I misunderstanding how this feature works?

@dmyers
Copy link
Author

dmyers commented Oct 12, 2014

Sorry for the delay I think what I was testing with was something like this. I tried with and without a bucket name since I think it is supposed to be passed through the CNAME.

$client = \S3Client::factory(
    'key' => $aws_key,
    'secret' => $aws_secret,
    'base_url' => 'http://s3.example.com',
);
$url = $client->getObjectUrl('', 'test');
echo $url;

@kara-todd
Copy link

Per the conversation on Gitter with @jeremeamia it was explained that changing the getObjectUrl call to the following fixed this issue:

$client = S3Client::factory(
    'key' => $key,
    'secret' => $secret,
    'base_url' => 'http://s3.example.com/my-bucket/',
);
$url = $client->getObjectUrl('my-bucket', 'file.txt', null, ['PathStyle' => true])

So the url would now resolve as: http://s3.example.com/my-bucket/file.txt

@jeremeamia
Copy link
Contributor

That is almost right. You should set the 'base_url' to 'http://s3.example.com' and leave off the 'my-bucket/'.

The 'PathStyle' param is needed to stop the S3Client from putting the bucket into the hostname. Normally, the SDK does this automatically, because this is how normal virtual-style bucket addressing works in S3. Using the 'PathStyle' param tells the SDK to leave the bucket in the path.

@yellow1912
Copy link

This workaround does allow me to use getObjectUrl correctly, but the upload feature won't work till I applies another trick posted also by @jeremeamia
https://gist.github.com/leftnode/e3107493f078516555f3

Hopefully this will help someone saves hours of fiddling with the code.

@yellow1912
Copy link

Unfortunately I celebrated too early. It seems like by setting host I generate another error:

"The request signature we calculated does not match the signature you provided. Check your key and signing method."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants