簡體   English   中英

在 PHP 上使用帶有 IP 的 getIterator 時,AWS S3Client 解決了錯誤的 url

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

我無法使用 S3Client 的 getIterator function,因為它以某種方式反轉了 url。

而不是它尋找http://192.168.120.70/bucket它返回這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM