AWS S3Client 在 PHP 上使用带有 IP 的 getIterator 时解决了错误 url

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

我无法使用 S3Client 的 getIterator 函数,因为它以某种方式反转了 url。

而不是寻找 http://192.168.120.70/bucket 它 returns 这个:

Could not resolve host: bucket.192.168.120.70

我确定我忽略了一些简单的事情。

<?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
?>

完整错误:

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

这就是您实现列出存储桶的目标所需的全部内容。端点选项用于特定服务,在此场景中不需要:

    $client = S3Client::factory(
        array(
            'credentials' => array(
                'key' => $IAM_KEY,
                'secret' => $IAM_SECRET,
            ),

            'region' => 'eu-west-1',
            'version' => 'latest')
    );

我认为这是我创建 s3client 时的一个问题,此后它起作用了。 奇怪的是,我之前的代码只是第一个桶没有工作。

// 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());
}