【问题标题】:appengine std PHP stream wrapper from storage来自存储的 appengine std PHP 流包装器
【发布时间】:2019-11-14 11:33:45
【问题描述】:

我正在编写一个带有表单的 php 网页,用户可以在其中上传一个 excel 文件。

我需要获取此文件并通过 SpreadsheetReader PHP 库读取它

在 appengine 中,不可能在文件系统上写入,所以我通过 streamwrapper 将此文件发送到 Cloud Storage 存储桶(运行良好)。

但我不知道如何从类似存储文件的对象中读取并加载到 SpreadsheetReader...

首先,按照我的部分代码,来自存储的流包装器不起作用:

$client = new StorageClient(['projectId' => $projectId,

                                ]);
$client->registerStreamWrapper();
$contents = file_get_contents($location);

返回此错误: “PHP 致命错误:未捕获的 InvalidArgumentException:json_encode 错误:格式错误的 UTF-8 字符,可能编码不正确”

2) 如何创建一个类似文件的对象来做到这一点:

$reader = new SpreadsheetReader($file);
$sheets = $reader->Sheets();

谢谢

【问题讨论】:

    标签: php google-app-engine storage phpspreadsheet


    【解决方案1】:
    $client = new StorageClient(['projectId' => $projectId,
    
                                    ]);
    $client->registerStreamWrapper();
    $contents = file_get_contents($location);
    

    返回此错误:“PHP 致命错误:未捕获 InvalidArgumentException:json_encode 错误:UTF-8 格式错误 字符,可能编码不正确”

    您应该以 JSON 格式在 StorageClient 中提供 keyFile。

    https://github.com/googleapis/google-cloud-php-storage/blob/master/src/StorageClient.php#L78

    【讨论】:

    • 谢谢!能写,为什么需要key文件才能读?
    • array $keyFile 从 Google Developer's Console 检索到的服务帐户凭据 * .json 文件的内容。 * 例如:json_decode(file_get_contents($path), true).
    • 感谢您的回复。现在没关系,但收到一个空内容。你有什么想法吗?
    【解决方案2】:

    我了解到您正在通过 streamwrapper 将文件写入 Cloud Storage 存储分区。

    然后您尝试使用以下命令读取文件:

     $contents = file_get_contents($location);
    

    根据官方文档link

    但是,如果应用需要读取文件,而这些文件是静态的, 您可以选择使用 PHP 读取与您的应用一起上传的静态文件 文件系统函数,例如 file_get_contents。

    例如:

    $fileContents = file_get_contents($filePath);

    其中指定的路径必须是相对于脚本的路径 访问它们。

    你必须上传一个或多个应用程序子目录中的文件,当 您将应用部署到 App Engine,并且必须配置 app.yaml 文件,以便您的应用可以访问这些文件。有关完整的详细信息,请参阅 PHP 5 使用 app.yaml 配置应用程序。

    我认为您想从 GCS 读取文件,而不是从应用程序子目录 link

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 2012-07-28
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 2014-11-11
      • 1970-01-01
      相关资源
      最近更新 更多