【问题标题】:PHP Google Cloud StoragePHP 谷歌云存储
【发布时间】:2014-05-24 09:57:48
【问题描述】:

我正在尝试在我的 PHP 项目中使用 Google Cloud Storage,但不在 AppEngine 平台上。我发现的所有链接和教程都使用 AppEngine 平台,因此可以使用 gs:// 链接。有谁知道如何连接并例如列出存储桶中的对象 - 或者至少在没有 AppEngine 的情况下通过 php 连接到存储槽?

【问题讨论】:

  • 我不知道这些是否使用“AppEngine”,但它们似乎确实提供了从 PHP 访问“谷歌云存储”的权限。我从未使用过它们:storage/docs/json_api/v1/libraries.
  • 由于项目要求,我无法使用 appEngine。但是我确实设法连接 - 但仍然没有直接从 php 使用它的示例:/(有其他 API 的示例,但没有用于云存储的示例)
  • 您考虑过 Amazon S3 吗?它的 PHP API 非常好,您可以轻松注册一个 PHP 流包装器 s3://
  • 是的,我使用它 - 它就像一个魅力。这个项目请求都是谷歌(sql、storage、g+...)——你会认为这样一个拥有强大工具的大公司也会有一个很好的文档,但没有:/

标签: php google-cloud-storage


【解决方案1】:

您可以使用JSON api

要查看它的实际效果,请点击此处:

https://developers.google.com/apis-explorer/#p/storage/v1/storage.objects.list

然后在右上角启用 OAuth2 并填写您的存储桶名称以查看示例请求和响应。

【讨论】:

  • 嗨,谢谢你的回答,但我确实找到了样本。我询问了 php 客户端的 API 文档。
【解决方案2】:

试试这个代码:

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once("Google/Service/Storage.php");

$client_id = '<client-id>'; //Client ID
$service_account_name = '<service_account_name(email)>'; //Email Address 
$key_file_location = 'key.p12'; //your key.p12 file

echo pageHeader("Service Account Access");
if ($client_id == '<YOUR_CLIENT_ID>'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}

$client = new Google_Client();
$client->setApplicationName("<your application name>");
$service = new Google_Service_Storage($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/devstorage.full_control'),
    $key
);

$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

$acl = new Google_Service_Storage_ObjectAccessControl();
$acl->setEntity('allUsers');
$acl->setRole('READER');
$acl->setBucket('<your bucket name>');
$acl->setObject('your object name');

$service = new Google_Service_Storage($client);
$return_value = $service->objects->get('<bucket_name>','<object-name>');

【讨论】:

  • 谢谢,我现在已经写了一些接近这个的东西(解决了)所以我目前无法测试它,但它仍然会帮助其他正在搜索相同代码的人:)跨度>
猜你喜欢
  • 2020-09-10
  • 2016-10-12
  • 2016-12-30
  • 1970-01-01
  • 2013-07-03
  • 2012-08-19
  • 2017-01-11
  • 1970-01-01
相关资源
最近更新 更多