3

Ok, so I have tried several methods of getting a file to upload to my S3 account. Finally found myself getting somewhere and BOOM - confusing documentation and strange error messages which appear to contradict themselves.

Ok, to start with I am not using composer or anything like that, I am doing this the old fashioned way:

require '/path/to/aws-autoload.php';

Now this loads correctly, and I have trimmed down the autoload to only use Common and S3 classes - don't need everything!

Next up I load the S3 Client and Credentials:

use Aws\S3\S3Client;
use Aws\Common\Credentials\Credentials;

Now the code to start the magic happening:

$file = '/path/to/' . $filename;
$credentials = new Credentials('KEY', 'SECRET KEY');
$s3 = S3Client::factory(array('credentials' => $credentials));

try{
$s3->putObject(array(
    'Bucket' => 'sub.domain.com.s3.amazonaws.com',
    'Body'   => fopen($file, 'r'),
    'ACL'    => 'public-read',
));
} catch (S3Exception $e) {
     $result = array(
         'ok' => false,
         'descr' => 'There was an error uploading the file to S3 : ' . $filename
     );
}

The issue I seem to be having is with the 'Bucket' itself.

When I format the Bucket as sub.domain.com I get the following message back from the AWS API:

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: "sub.domain.com.s3.amazonaws.com".

Now when I change the 'Bucket' to match the above like so : sub.domain.com.s3.amazonaws.com

I get the following error message :

AWS Error Message: The specified bucket does not exist

Am I doing something wrong? Is something missing? Unfortunately the AWS Documentation is not quite up to scratch. The API seems to be contradicting itself at the moment. I know all the keys are correct, and all permissions are correct. It went from 301 - Redirect to 404 - Not Found from its own advice.

Any help/advise would be greatly appreciated. I feel like I am going in circles a little here!

9
  • Isn't just the sub.domain part used as an example? I think you should change this value to whatever you named your bucket in the first place. Commented Oct 7, 2014 at 9:24
  • I have already done this, I set this to sub.domain when posting on here to make this more generic and not give away the site I work for. Also The KEY and SECRET KEY are set to the correct values Commented Oct 7, 2014 at 9:27
  • Have you tried accessing your bucket using a path-style URL like http://s3.amazonaws.com/<bucket>? Commented Oct 7, 2014 at 9:31
  • Yep, same result : <Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message><Bucket>sub.domain.com</Bucket><Endpoint>sub.domain.com.s3.amazonaws.com</Endpoint></Error> Commented Oct 7, 2014 at 9:33
  • 1
    Try using the php s3 client to check so the name of your bucket is valid for an S3 bucket, isValidBucketName($bucketName);, I'm thinking that the problem might be the format of the name of the bucket. Commented Oct 7, 2014 at 9:44

1 Answer 1

4

Here are some things to double check.

  1. If the bucket was created in a certain region (e.g., us-west-2), instantiate the S3Client with that region [..., 'region' => 'us-west-2', ...].
  2. The 'Bucket' parameter should be set to exactly what your bucket is named (e.g., if your bucket is named "sub.domain.com", then the 'Bucket' parameter should be set to 'sub.domain.com'). Do not include the region or "s3.amazonaws.com" in the 'Bucket' parameter (i.e., bucket != endpoint). The SDK automatically determines the endpoint (based on the client's region and provided bucket name) and adjusts the URL for path-style if needed.
  3. The PutObject operation requires the 'Key' parameter, which is not present in the code sample above.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.