我正在尝试通过PHP将文件上传到S3存储桶。这在过去一直有效,我相信由于PHP版本控制现在无法正常工作,但不确定。我有GuzzleHTTP和AWS子文件夹。我现在收到以下错误:GuzzleHttp / Psr7 / functions.php,errline:417,errstr:未捕获错误:调用未定义函数GuzzleHttp \ Psr7 \ hash_init()。
我确实发现7.2中的hash_init进行了一些更改,因此我回滚到7.1并仍然出现错误。
<?php
require 'aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
function image_to_s3($fileName) {
// 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(
'credentials' => array(
'key' => 'KEY',
'secret' => 'SECRET'
),
'version' => 'latest',
'region' => 'us-east-2'
)
);
} 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.
return $e->getMessage();
}
// prep the aws s3 bucket
$bucket = 'BUCKETNAME';
$keyname = $fileName;
$filepath = 'SUBDIRECTORY/' . $fileName;
echo $filePath;
try {
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
} catch (S3Exception $e) {
return $e->getMessage();
} catch (Exception $e) {
return $e->getMessage();
}
return '';
}
?>
完整堆栈跟踪...
Fatal error: errno: 1, errfile: \/home\/USERNAME\/GuzzleHttp\/Psr7\/functions.php, errline: 417, errstr: Uncaught Error: Call to undefined function GuzzleHttp\\Psr7\\hash_init() in \/home\/USERNAME\/GuzzleHttp\/Psr7\/functions.php:417\nStack trace:\n#0 \/home\/USERNAME\/Aws\/Signature\/SignatureV4.php(164): GuzzleHttp\\Psr7\\hash(Object(GuzzleHttp\\Psr7\\LazyOpenStream), 'sha256')\n#1 \/home\/USERNAME\/Aws\/Signature\/S3SignatureV4.php(22): Aws\\Signature\\SignatureV4->getPayload(Object(GuzzleHttp\\Psr7\\Request))\n#2 \/home\/USERNAME\/Aws\/Middleware.php(126): Aws\\Signature\\S3SignatureV4->signRequest(Object(GuzzleHttp\\Psr7\\Request), Object(Aws\\Credentials\\Credentials))\n#3 \/home\/USERNAME\/GuzzleHttp\/Promise\/FulfilledPromise.php(39): Aws\\Middleware::Aws\\{closure}(Object(Aws\\Credentials\\Credentials))\n#4 \/home\/USERNAME\/GuzzleHttp\/Promise\/TaskQueue.php(47): GuzzleHttp\\Promise\\FulfilledPromise::GuzzleHttp\\Promise\\{closure}()\n#5 \/home\/USERNAME\/GuzzleHttp\/Promise\/Promise.php(246): GuzzleHttp\\Promise\\TaskQueue->run("}
答案 0 :(得分:0)
MatsLindh带我走了正确的路。 Hash现在已内置到PHP中,但是我的测试环境托管提供商(DreamHost)将enable-hash设置为shared,这意味着每个用户都需要通过导入Shared Object文件来手动启用扩展。我是通过将以下命令添加到我的php.ini文件extension = hash.so
中来完成此操作的,您可以立即知道扩展名正确加载,因为您将向phpinfo()添加了一个用于哈希的全新表。