简体   繁体   English

PHP Google云端存储

[英]PHP Google Cloud Storage

I'm trying to use Google Cloud Storage in my PHP project - but not on the AppEngine platform. 我正在尝试在我的PHP项目中使用Google云端存储 - 但不是在AppEngine平台上。 All the links and tutorials I found always use the AppEngine platform and therefor can use the gs:// links. 我发现的所有链接和教程总是使用AppEngine平台,因此可以使用gs://链接。 Does anyone know how to connect and for example list objects in a bucket - or at least connect to the storage trough php without AppEngine? 有没有人知道如何连接和例如列表中的对象 - 或者至少在没有AppEngine的情况下通过php连接存储?

try this code: 试试这段代码:

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>');

You can use the JSON api . 您可以使用JSON api

To see it in action go here: 要看到它的实际效果,请点击此处:

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

Then enable OAuth2 in the top right and fill in your bucket name to see an example request and response. 然后在右上角启用OAuth2并填写您的存储桶名称以查看示例请求和响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM