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!
PermanentRedirect