【问题标题】:Cannot connect to s3 from ec2 via php sdk - cURL error 7无法通过 php sdk 从 ec2 连接到 s3 - cURL 错误 7
【发布时间】:2021-09-22 21:49:03
【问题描述】:

我的 EC2 实例连接到我们的 S3 存储桶时遇到问题。

在我的 ec2 实例上,我安装了 php、httpd、aws-cli、aws-sdk。我能够访问代码所在的索引页面。凭据文件设置 /usr/share/httpd/.aws/credentials

我使用的代码来自Getting Started Basic Usage Guide

$sharedConfig = [
    'profile' => 'default',
    'region' => 'us-west-1',
    'version' => 'latest',
    'debug' => true
];

// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);

// Use an Aws\Sdk class to create the S3Client object.
$s3Client = $sdk->createS3();

// Send a PutObject request and get the result object.
$result = $s3Client->putObject([
    'Bucket' => 'my-bucket',
    'Key' => 'my-key',
    'Body' => 'this is the body!'
]);

使用调试模式会导致大量文本转储。我认为主要错误属于 s3 存储桶拒绝来自代码的连接。

Entering step attempt, name 'ApiCallAttemptMonitoringMiddleware' ------
 no changes * Trying IP_ADDRESS... * TCP_NODELAY set * Immediate connect fail for IP_ADDRESS: Permission denied * 
Closing connection 0 <- Leaving step attempt, name 'ApiCallAttemptMonitoringMiddleware' --------- error was set to array(13) { ["instance"]=> string(32) "0000000078dd756a0000000053d619cb" ["class"]=> string(28) "Aws\S3\Exception\S3Exception" ["message"]=> string(241) "Error executing "PutObject" on "https://ec2-server-test.s3.us-west-1.amazonaws.com/my-key"; 
AWS HTTP error: cURL error 7: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://ec2-server-test.s3.us-west-1.amazonaws.com/my-key" ["file"]=> string(58) "/var/www/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php" ["line"]=> int(195) ["trace"]=> string(1925) "#0 /var/www/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php(100)

Curl 错误 7:无法 connect() 到主机或代理。

通过 SSH 连接到我的实例时,我能够正确运行 aws-cli s3 命​​令并收到正确的响应。

此代码也可以在我通过 xampp 托管的机器上本地运行。

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-ec2 aws-cli aws-php-sdk


    【解决方案1】:

    问题是网络服务器 httpd 无法与外部网络通信。这就是为什么我可以从命令行使用 aws cli 和 telnet/ ping,但无法从浏览器访问。

    运行命令 setsebool -P httpd_can_network_connect 1 后,我能够连接到我的 s3 存储桶。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2017-11-09
      • 2023-03-26
      相关资源
      最近更新 更多