Search code examples
phpamazon-web-servicesamazon-s3hostresolve

AWS S3Client resolves the wrong url when using getIterator with an IP on PHP


I am unable to use the getIterator function of S3Client due to it somehow reversing the url.

Instead of it looking for http://192.168.120.70/bucket it returns this:

Could not resolve host: bucket.192.168.120.70

I'm sure there is something simple I'm overlooking.

<?php
    require '/Applications/MAMP/htdocs/lab/aws/aws-autoloader.php';
    use Aws\S3\S3Client;
    use Aws\Exception\AwsException;

    $bucketName = 'bucket';
    $IAM_KEY = 'MY-KEY';
    $IAM_SECRET = 'MY-SECRET';

    // Connect to AWS
    try {
        $s3 = S3Client::factory(
            array(
                'credentials' => array(
                    'key' => $IAM_KEY,
                    'secret' => $IAM_SECRET
                ),
                'version' => 'latest',
                'region'  => 'eu-west-1',
                'endpoint' => 'http://192.168.120.70/',
                'profile' => 'MY-PROFILE'
            )
        );
    } catch (Exception $e) {
        die("Error: " . $e->getMessage());
    }

    $buckets = $s3->listBuckets();
    foreach ($buckets['Buckets'] as $bucket) {
        echo $bucket['Name'] . "\n";
    }
    // returns -> bucket

    $obj = $s3->getIterator('ListObjects', array('Bucket' => 'bucket'));
    foreach ($obj as $object) {
        var_dump($object);
    }
    // Error -> Could not resolve host: bucket.192.168.120.70
?>

The full error :

Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing 
"ListObjects" on "http://bucket.192.168.120.70/?encoding-type=url"; 
AWS HTTP error: cURL error 6: Could not resolve host: bucket.192.168.120.70 (see 
https://curl.haxx.se/libcurl/c/libcurl-errors.html)' GuzzleHttp\Exception\ConnectException: cURL 
error 6: Could not resolve host: bucket.192.168.120.70 
(see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in 
/Applications/MAMP/htdocs/lab/aws/GuzzleHttp/Handler/CurlFactory.php:200 Stack trace: #0 
/Applications/MAMP/htdocs/lab/aws/GuzzleHttp/Handler/CurlFactory.php(155): 
GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 
/Applications/MAMP/htdocs/lab/aws/GuzzleHttp/Handler/CurlFactory.php(105): 
GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlMultiHandler), 
Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 
/Applications/MAMP/htdocs/lab/aws/GuzzleHttp/Han in 
/Applications/MAMP/htdocs/lab/aws/Aws/WrappedHttpHandler.php on line 195

Solution

  • I seems it was an issue in my s3client creation, after this it worked. Strangely with my previous code it was only the first bucket that wasn't working.

    // Connect to AWS
    try {
        // You may need to change the region. It will say in the URL when the bucket is open
        // and on creation.
        $s3 = S3Client::factory(
            array(
                'version' => 'latest',
                'region'  => 'Standard',
                'endpoint' => 'http://192.167.120.60/',  // Needed in my case
                'use_path_style_endpoint' => true,       // Needed in my case
                'credentials' => array(
                    'key' => $IAM_KEY,
                    'secret' => $IAM_SECRET
                )
            )
        );
    } catch (Exception $e) {
        // We use a die, so if this fails. It stops here. Typically this is a REST call so this would
        // return a json object.
        die("Error: " . $e->getMessage());
    }