【问题标题】:S3 file upload from PHP error: "Call to undefined function GuzzleHttp\Psr7\hash_init()"从 PHP 错误上传 S3 文件:“调用未定义函数 GuzzleHttp\Psr7\hash_init()”
【发布时间】:2019-11-08 20:43:20
【问题描述】:

我正在尝试通过 PHP 将文件上传到 S3 存储桶。这在过去一直有效,我相信由于 PHP 版本控制它现在无法正常工作,但不确定。我有 GuzzleHTTP 和 AWS 子文件夹。我现在收到以下错误:GuzzleHttp/Psr7/functions.php, errline: 417, errstr: Uncaught Error: Call to undefined function 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("}

【问题讨论】:

  • 您确定已启用哈希扩展吗?构建包时可以显式禁用它。 phpinfo(); 应该告诉你。
  • 这是 phpinfo 中唯一似乎相关的行,它位于第二行 'Configure Command' ...--enable-hash=shared'...
  • 在这种情况下,您必须显式安装哈希扩展。这取决于您使用的发行版或操作系统。
  • 共享是否意味着没有安装?
  • 由于缺乏声誉而将答案添加为评论:@MatsLindh 引导我走上了正确的道路。哈希现在内置在 PHP 中,但是我的测试环境 (DreamHost) 的托管服务提供商将启用哈希设置为共享,这意味着每个用户都需要通过导入共享对象文件来手动启用扩展。我通过将以下命令添加到我的 php.ini 文件中来做到这一点 ``` extension = hash.so ``` 只要扩展正确加载,您就可以知道,因为您将在 phpinfo() 中添加一个全新的表散列。

标签: php amazon-s3


【解决方案1】:

MatsLindh 带领我走上了正确的道路。哈希现在内置在 PHP 中,但是我的测试环境 (DreamHost) 的托管服务提供商将启用哈希设置为共享,这意味着每个用户都需要通过导入共享对象文件来手动启用扩展。我通过将以下命令添加到我的 php.ini 文件中来做到这一点extension = hash.so 您可以在扩展正确加载后立即知道,因为您将在 phpinfo() 中添加一个全新的表以进行哈希。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多