Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)
edited by

Could anyone tell me how to programmatically upload a file to a bucket that's set to be a static website in the S3 console using AWS SDK? The bucket name is foo.ourdomain.com and is hosted in EU-west. I am using the following code to upload the file: 

$client = \Aws\S3\S3Client::factory(array('key' => bla, 'secret' => bla));

  $client->upload('foo.ourdomain.com', 'test.txt', 'hello world', 'public-read');

But upon using it, I got an exception: 

PHP Fatal error: Uncaught Aws\S3\Exception\PermanentRedirectException: AWS Error Code: PermanentRedirect, Status Code: 301, AWS Request ID: -, AWS Error Type: client, AWS Error Message: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint: "foo.ourdomain.com.s3.amazonaws.com"., User-Agent: aws-sdk-php2/2.4.8 Guzzle/3.7.4 curl/7.22.0 PHP/5.3.10-1ubuntu3.8

Since there was no mention of this in the S3 SDK manual, I found a method setEndpoint and adjusted the code as: 

   $client = \Aws\S3\S3Client::factory(array('key' => bla, 'secret' => bla));

   $client->setEndpoint('foo.ourdomain.com.s3.amazonaws.com');

   $client->upload('foo.ourdomain.com', 'test.txt', 'hello world', 'public-read');

Since I tried using both the methods, I am still getting the same error, could anybody suggest me a method to fix this, since I am not getting any solutions to it.

1 Answer

0 votes
by (44.4k points)

Changing the initialization of the client is the solution:

$client = \Aws\S3\S3Client::factory(array('key' => demo, 'secret' => demo, 'region' => 'eu-west-1'));

Instead of specifying an endpoint, you can set the region in the options array itself.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...