【问题标题】:Access Google Cloud Storage from Local Env PHP从本地环境 PHP 访问 Google Cloud Storage
【发布时间】:2017-11-14 12:08:48
【问题描述】:

我只是想做一个很简单的file_get_contents('gs://[bucket][file]');

我在本地运行时收到此错误。
"\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper::stream_open" call failed

但是,当我在生产环境中运行它时,它运行良好。

我知道我在设置中遗漏了一些东西,但我不确定是什么。

我已经设置了项目gcloud config set project PROJECT_ID,设置了账号gcloud config set account ACCOUNT,运行gcloud auth login。关于我在这里的设置中缺少什么的任何想法?


我运行了composer installcomposer require google/cloud,在包含use Google\Cloud\Storage\StorageClient; 时没有收到任何错误我创建了service-account-keyfile 作为存储桶的所有者。将所有者设置为完全权限。跑这段代码...

require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
    'keyFilePath' => __DIR__ .'/../keys/appdocs-com.json',
]);
echo file_get_contents('gs://[myBucket][myFile]');

我仍然收到...streamWrapper::stream_open" call failed

我什至尝试了服务构建器方法。

require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\ServiceBuilder;
use Google\Cloud\Storage\StorageClient;

$gcloud = new ServiceBuilder([
    'keyFilePath' => __DIR__ .'/../keys/appdocs-com.json',
    'projectId' => 'appdocs-com',
]);

$storage = $gcloud->storage();
$bucket = $storage->bucket('test-appdocs-sendgrid-inbound');
$object = $bucket->object('4/envelope.json');
$stream = $object->downloadAsStream();
echo $stream->getContents();

这会返回

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message 'No system CA bundle could be found in any of the the common system locations. PHP versions earlier than 5.6 are not properly configured to use the system's CA bundle by default. In order to verify peer certificates, you will need to supply the path on disk to a certificate bundle to the 'verify' request option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not need a specific certificate bundle, then Mozilla provides a commonly used CA bundle which can be downloaded here (provided by the maintainer of cURL): https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP ini setting to point to the path to the file, allowing you to omit the 'verify' request option. See http://curl.haxx.se/docs/sslcerts.html for more information.' in /Users/thom/Engine/appdocs-com/vendor/google/cloud-core/RequestWrapper.php:241 Stack trac in /Users/thom/Engine/appdocs-com/vendor/google/cloud-core/RequestWrapper.php on line 241

我在php55 有我的runtime。这会阻止我使用StorageClientserviceBuilder

【问题讨论】:

  • 您应该确保您的开发环境中的 gcloud 和 php 安装和配置与生产环境匹配
  • 如果我使用gcloud app deploy从命令行部署那个应用程序,它们不应该匹配吗?

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


【解决方案1】:

在 appengine 环境之外,您将需要手动注册云存储流包装器以使用gs:// 协议访问文件:

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'keyFilePath' => '/path/to/service-account-keyfile.json'
]);

$storage->registerStreamWrapper();

此示例假设您已安装 Google Cloud PHP

更多信息请访问Google Cloud PHP docs

要解决 guzzle ssh 错误,请参阅this answer注意verify 设置为false 是不安全的,只能用于测试,切勿用于生产。

【讨论】:

  • 我更新了我的答案以显示我尝试过的内容......你真的有这个工作吗?
  • 我也更新了我的。您现在看到的错误与您的 curl 安装未正确配置以处理安全请求有关。这是本地开发服务器中相当常见的问题,可以通过将php.ini 文件中的openssl.cafile 设置指向有效证书来永久解决。
  • 我正在阅读cloud.google.com/appengine/docs/standard/php/config/php_ini,看起来他们关闭了 curl。我不知道有没有办法解决这个问题。
【解决方案2】:

正如@jdp 在他的回答中提到的,我遇到的错误与我安装 curl 有关。

所以我查看了 Google 的 php_ini 文档,看看是否可以设置 openssl.cafile。 https://cloud.google.com/appengine/docs/standard/php/config/php_ini

如果您想使用 Google Storage Client,您需要从标准环境更改为灵活环境。 https://cloud.google.com/appengine/docs/flexible/php/quickstart

然后阅读云存储文档以了解如何下载 API 使用的依赖项:https://cloud.google.com/storage/docs/reference/libraries

一旦您设置了 flex env 并收集了依赖项,您就可以按照这些说明设置连接。

在 Google Cloud Console 中设置服务帐号密钥:

  • 选择“您的项目”->IAM & ADMIN-> 服务帐户。
  • 点击“创建服务帐户”按钮。
  • 分配角色(在我的例子中,我分配了角色“所有者”,因为此服务帐户是我本地开发的专用帐户),选中复选框“提供新的私钥”。

这刚刚下载了你的“service-account-keyfile.json”

然后在代码中像这样启动云存储:

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'keyFilePath' => '/path/to/service-account-keyfile.json'
]);

$storage->registerStreamWrapper();

您现在可以像从生产环境中一样检索存储桶数据。

echo file_get_contents('gs://[Bucket][File]');

确保您已将存储桶权限设置为允许之前选择的角色访问该存储桶。

但是,我仍然很想找到一种方法来设置具有 php 标准的本地环境,该环境可以与 gs://[bucket]/[file] 交互。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2021-11-14
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多