我正在尝试按照我在YouTube上找到的教程
将文件从php脚本上传到s3我有以下php文件
upload.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>
Start.php
'最新', 'region'=&gt; '美东-1' ]); ?&GT;的config.php
<?php
return [
's3' => [
'bucket' => 'umnbucket2016'
]
];
?>
我在〜/ .aws /凭证的文件包含[默认]而不是我的密钥和密钥
当我尝试上传文件时,我收到以下错误
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存储我的密钥和秘密ID,但改为支持凭证文件
任何帮助将不胜感激。
答案 0 :(得分:2)
你得到的错误是一个线索:
的fopen(http://169.254.169.254/latest/meta-data/iam/security-credentials/): 无法打开流:连接超时
该库正在尝试访问安全凭证元数据,但正在超时。
您能否确认可以从实例内部打开该URL?当然,此代码应位于正在运行的AWS实例上,并且应该可以访问元数据URL。
可能存在导致该网址被阻止的问题。防火墙吗?安全组?