php - 从实例配置文件元数据服务器检索凭据时出错

标签 php amazon-web-services amazon-s3 aws-sdk

我正在尝试按照我在 YouTube 上找到的教程将文件从 php 脚本上传到 s3

我有以下 php 文件

上传.php

use Aws\S3\Exception\S3Exception;

require 'start.php';

if(isset($_FILES['file'])){
    $file=$_FILES['file'];

    $name = $file['name'];
    $tmp_name = $file['tmp_name'];

    $file_ext = explode('.', $name);
    $file_ext = strtolower(end($file_ext));

    $key = md5(uniqid());
    $tmp_file_name = "{$key}.{$file_ext}";
    $tmp_file_path = "../files/{$tmp_file_name}";

    move_uploaded_file($tmp_name, $tmp_file_path);
    try{
        $s3 -> putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => 'uploads/{$key}.{$name}',
                'Body' => fopen($tmp_file_path, 'rb'),
                'ACL' => 'public-read'

        ]);

        unlink($tmp_file_path);
    }catch(S3Exception $e){
        die("Error uploading file");
    }


}

?>

<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="file">

            <input type="submit" value="Upload">
        </form>
    </body>
</html>

开始.php

'最新的', '地区' => 'us-east-1' ]); ?>

配置.php

<?php

return [
    's3' => [
        'bucket' => 'umnbucket2016'
    ]
];
?>

我位于 ~/.aws/credentials 的文件包含[默认],而不是我的 key 和 secret key

当我尝试上传文件时,出现以下错误

 Fatal error: Uncaught exception 'Aws\Exception\CredentialsException' with message 'Error retrieving credentials from the instance profile metadata server. (Error creating resource: [message] fopen(http://169.254.169.254/latest/meta-data/iam/security-credentials/): failed to open stream: Connection timed out [file] /var/www/html/AWS/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 282)' in /var/www/html/AWS/vendor/aws/aws-sdk-php/src/Credentials/InstanceProfileProvider.php:79 Stack trace: #0 /var/www/html/AWS/vendor/guzzlehttp/promises/src/Promise.php(199): Aws\Credentials\InstanceProfileProvider->Aws\Credentials\{closure}(Array) #1 /var/www/html/AWS/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\Promise\Promise::callHandler(2, Array, Array) #2 /var/www/html/AWS/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #3 /var/www/html/AWS/vendor/guzzlehttp/promises/src/Promise.php(240): GuzzleHttp\Promise\TaskQueue->run(true) #4 /var/www/html/AWS in /var/www/html/AWS/vendor/aws/aws-sdk-php/src/Credentials/InstanceProfileProvider.php on line 79   

我最初让 config.php 存储我的 key 和 secret ID,但更改了它以支持凭证文件

如有任何帮助,我们将不胜感激。

最佳答案

您收到的错误是一条线索:

fopen(http://169.254.169.254/latest/meta-data/iam/security-credentials/): failed to open stream: Connection timed out

库正在尝试访问安全凭证元数据,但超时。

您能否确认可以从实例内部打开该 URL?当然,此代码应该位于工作的 AWS 实例上,并且元数据 URL 应该可以访问。

可能存在某些问题导致该 URL 被阻止。防火墙?安全组?

关于php - 从实例配置文件元数据服务器检索凭据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34684938/

相关文章:

javascript - 在表单中的 div 中提交 <select> 字段的值

amazon-web-services - AWS 负载均衡器中的持续后端连接错误

amazon-web-services - 我如何使用 GO 检查 aws s3 中是否已经存在 S3 存储桶

php - 部分布局错误

javascript - 如何从 mysql 数据库获取行值并通过单击单选按钮填充文本框而不刷新 php 页面?

matlab - 特征提取步骤花费的时间太长

amazon-web-services - 停止 EC2 实例时如何保留数据

ios - 通过 iOS amazon sdk 在 s3 上上传图片缓慢

python - AWS Lambda 错误消息 "Unable to import module ' lambda_function' : No module named 'lambda_function' ",

javascript - PHP 表单选择字段中自动补全用户姓氏