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!
Here are some things to double check.
S3Client
with that region [..., 'region' => 'us-west-2', ...]
.'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.'Key'
parameter, which is not present in the code sample above.