【问题标题】:GAE PHP application: Unable to find the wrapper "gs"GAE PHP 应用程序:找不到包装器“gs”
【发布时间】:2019-10-27 01:45:16
【问题描述】:

我正在 standard php73 Google App 引擎环境中编写一些非常简单的代码,遵循此处的文档:https://cloud.google.com/appengine/docs/standard/php/googlestorage/https://cloud.google.com/appengine/docs/standard/php/googlestorage/setup

php.ini(根据该场景的文档不需要,但以防万一)

google_app_engine.allow_include_gs_buckets = "#default#"

index.php:

file_put_contents("gs://#default#/hello.txt", "some text");

并从 Google App Engine 收到以下错误

file_put_contents(): Unable to find the wrapper "gs" - did you forget to enable it when you configured PHP?

据我从文档中可以看出,应该不需要其他配置,因为 GAE 会在其环境中自动注册文件流包装器。

我错过了什么?谢谢!

【问题讨论】:

    标签: php google-app-engine google-cloud-platform google-cloud-storage


    【解决方案1】:

    所以事实证明该文档是针对 PHP 5 环境而不是 PHP 7(尽管没有说明)。在这里记录了在 PHP 7 中实现此功能的方法:

    https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine/php72/storage/src

    我刚刚这样做了:

    use Google\Cloud\Storage\StorageClient; 
    function register_stream_wrapper($projectId) {   
        $client = new StorageClient(['projectId' => $projectId]);
        $client->registerStreamWrapper();
    }
    register_stream_wrapper("projectId");
    

    注册包装器。

    【讨论】:

      【解决方案2】:

      App Engine 应用程序,您无法写入部署应用程序的文件系统。您的应用程序可以从已部署的目录结构中读取任何文件,但不能写入该文件系统。相反,该应用程序可以使用 Google Cloud Storage (GCS) 来读取和写入文件。

      修改一些应用文件路径以指向您的 Cloud 项目中的 GCS 存储桶 修改app文件上传表单,让他们使用GCS接收上传的文件

      您可以选择使用 PHP 文件系统函数(例如 file_get_contents)读取随应用上传的静态文件。

      $fileContents = file_get_contents($filePath);

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

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

      https://cloud.google.com/appengine/docs/standard/php/googlestorage/

      在 app.yaml 配置中,请注意,如果您使用静态文件或目录处理程序(static_files 或 static_dir),您必须将 application_readable 设置为 true,否则您的应用将无法读取文件。但是,如果文件由脚本处理程序提供服务,则没有必要这样做,因为这些文件默认情况下可由脚本处理程序读取。

      第二种选择是在 App Engine 之外运行您的应用程序,Google 提供 XML 和 JSON API。 GCS 开发者网站上提供了文档、库和示例应用程序。

      【讨论】:

      • 我知道这一点 - 我正在按照文档重新写入 GCS 存储桶,并收到此错误。
      • 答案与问题无关
      猜你喜欢
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2018-03-23
      • 2021-05-14
      • 2013-04-18
      • 2020-11-28
      相关资源
      最近更新 更多