从本地Env PHP访问Google云端存储

时间:2017-06-12 21:45:56

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

我只是想做一个非常简单的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

runtime位于php55。这可能会阻止我使用StorageClientserviceBuilder

2 个答案:

答案 0 :(得分: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并不安全,只应用于测试,永远不会投入生产。

答案 1 :(得分:0)

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

所以我查看了Google的php_ini文档,看看我是否可以设置openssl.ca文件。 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环境并收集依赖关系后,可以按照这些说明设置连接。

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

  • 选择“您的项目” - > IAM&管理 - >服务帐户。
  • 点击“创建服务帐户”按钮。
  • 分配角色(在我的情况下,我指定了角色“所有者”,因为此服务帐户特定于我的本地开发),请选中“提供新私钥”复选框。

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

然后在代码中启动云存储,就像这样     使用Google \ Cloud \ Storage \ StorageClient;

BeanCreationException: Error creating bean with name 'initH2TCPServer' ... 
Factory method 'initH2TCPServer' threw exception ... Exception opening port "19092" (port may be in use)

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

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

$storage->registerStreamWrapper();

确保已设置存储桶权限以允许先前选定的角色访问存储桶。

但是,我仍然希望找到一种方法来设置一个可以与echo file_get_contents('gs://[Bucket][File]'); 交互的php标准的本地环境。