首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP Google云存储

PHP Google云存储
EN

Stack Overflow用户
提问于 2014-05-24 09:57:48
回答 2查看 2.1K关注 0票数 3

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-03 19:50:23

试试下面的代码:

代码语言:javascript
复制
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>');
票数 4
EN

Stack Overflow用户

发布于 2014-05-24 22:40:43

您可以使用JSON api

要想看到它付诸行动,就在这里:

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

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23843638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档