下面是如何使用最新版本的client library (v2.0)的下載對象的示例:
// Authenticate your API Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new Google_Service_Storage($client);
try {
// Google Cloud Storage API request to retrieve the list of objects in your project.
$object = $storage->objects->get($bucketName, $objectName);
} catch (Google_Service_Exception $e) {
// The bucket doesn't exist!
if ($e->getCode() == 404) {
exit(sprintf("Invalid bucket or object names (\"%s\", \"%s\")\n", $bucketName, $objectName));
}
throw $e;
}
// build the download URL
$uri = sprintf('https://storage.googleapis.com/%s/%s?alt=media&generation=%s', $bucketName, $objectName, $object->generation);
$http = $client->authorize();
$response = $http->get($uri);
if ($response->getStatusCode() != 200) {
exit('download failed!' . $response->getBody());
}
// write the file (or do whatever you'd like with it)
file_put_contents($downloadName, $response->getBody());
你可以找到其他樣本在此使用存儲API Samples Repository
還有一個更新的庫正在開發名爲gcloud-php,它有一些01致電Google雲端存儲時致電。
我正在使用一些舊庫的代碼示例。你的代碼很棒!謝謝! – rsebjara
您有關於如何上傳的示例嗎?這是我目前的問題。 – zundi