简体   繁体   中英

Keep on recieving error: AWS HTTP error: cURL error 6: when trying to upload file

Everytime I try to upload a file to my s3 bucket with sdk, I keep on getting the error (AWS HTTP error: cURL error 6). I have no idea why this is happening and I have no idea on how to fix it. Is there anything else I need to add to my code or specify? Is the problem just with the bucket or could it be with the user I made (IAM). I don't need a definitive answer I just need something that can narrow down the area I need to look in for the cause of the error. I'd gladly appreciate any response. Thanks.

This is my code:

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;

// Instantiate an Amazon S3 client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'US-West', 
    'credentials' => [
        'key'    => 'garbage',
        'secret' => 'garbage'
    ]
]);


$bucketName = 'garbage';
$file_Path = __DIR__ . '/my-image.png';
$key = basename($file_Path);

// Upload a publicly accessible file. The file size and type are determined by the SDK.
try {
    $result = $s3->putObject([
        'Bucket' => $bucketName,
        'Key'    => 'videouploads/' . $key,
        'Body'   => fopen($file_Path, 'r'),
        'ACL'    => 'public-read',
    ]);
    echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
    echo $e->getMessage();
}


?>

That error corresponds to a could not resolve host error. See CURLE_COULDNT_RESOLVE_HOST (6) in https://curl.haxx.se/libcurl/c/libcurl-errors.html . This indicates that the lib could not reach the S3 endpoint referenced by the code.

Checking the code, it appears that the region definition is incorrect. You have defined the region as 'region' => 'US-West' . There is no such region. Using the wrong region name likely explains the host not found error. It's trying to reach a hostname in a region which does not exist, and generates the error 6.

Replace that region value with us-west-1 or us-west-2 and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM