AWS PHP SDK upload file to S3 obfuscating errors

So, I tried several ways to get the file to upload to my S3 account. I finally ended up somewhere and BOOM is confusing documentation and weird error messages that seem to contradict themselves.

Okay, for starters, I'm not using composer or anything like that, I do it the old fashioned way:

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

      

Now this is loading correctly and I have truncated the autoloading to only use the Common and S3 classes - not needing everything!

Next, I download the S3 client and credentials:

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

      

Now the code to run the magic:

$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 problem I seem to have is with the "bucket" itself.

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

AWS error message: The bucket you are trying 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: sub.domain.com.s3.amazonaws.com

The following error message appears:

AWS error message: bucket specified does not exist

Am I doing something wrong? Is there something missing? Unfortunately, the AWS Documentation isn't entirely scratched. It seems like the API is currently contradicting itself. I know all keys are correct and all permissions are correct. He went from 301 - Redirect to 404 - Not found by his own advice.

Any help / advice would be greatly appreciated. I feel like I'm going to be in circles here for a bit!

+3


source to share


1 answer


Here are a few things to double check.



  • If the bucket was created in a specific area (for example, us-west-2), create an instance S3Client

    with that area [..., 'region' => 'us-west-2', ...]

    .
  • The parameter 'Bucket'

    should be set to exactly what your bucket is named (for example, if your bucket is called "sub.domain.com" then the parameter 'Bucket'

    should be set to 'sub.domain.com'

    ). Do not include region or "s3.amazonaws.com" in the parameter 'Bucket'

    (ie Bucket! = Endpoint). The SDK automatically detects the endpoint (based on the client region and the bucket name provided) and configures the URL for the path style as needed.
  • The PutObject operation requires a parameter 'Key'

    that is missing in the example code above.
+4


source







All Articles