I'm having an issue trying to download a file from Google Cloud Storage using the php client found at https://code.google.com/p/google-api-php-client/
我在尝试使用https://code.google.com/p/google-api-php-client/上的php客户端从Google云端存储下载文件时遇到问题
I have authenticated myself ok and using the following code I can return an object which contains the link to my file
我已经验证了自己确定并使用以下代码我可以返回一个包含我的文件的链接的对象
$this->storageService = new Google_StorageService($this->client);
$this->objects = $this->storageService->objects;
$options = array(
'prefix' => 'REPORT_NAME_2013-07-01'
);
$bucket_contents = $this->objects->listObjects($bucket, $options);
The response is something like...
响应就像......
{
"kind": "storage#object",
"id": "<bucket>/<report>.csv/1001",
"selfLink": "https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv",
"name": "<report>.csv",
"bucket": "<bucket>",
"generation": "1001",
"metageneration": "1",
"contentType": "application/csv",
"updated": "2013-07-22T10:21:08.811Z",
"size": "806",
"md5Hash": "wT01i....",
"mediaLink": "https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv?generation=1001&alt=media",
"owner": {
"entity": "user-00b........",
"entityId": "00b490......."
},
"crc32c": "8y........",
"etag": "CPjZ.........."
}
But how do I go about downloading the file using the Google PHP client...I can't use a file_get_contents as it has no knowledge of the authentication details. The best thing I have found is something that uses the Google_Client but the response simply contains meta data and no object/file content
但是如何使用Google PHP客户端下载文件...我不能使用file_get_contents,因为它不知道身份验证详细信息。我发现的最好的东西是使用Google_Client,但响应只包含元数据而没有对象/文件内容
$request = new Google_HttpRequest($object['selfLink']);
$response = $this->client->getIo()->authenticatedRequest($request);
3 个解决方案
#1
4
Old question, but it got me looking in the right direction. selfLink
is the link to the metadata request, you need mediaLink
to get the actual object data, and it's getAuth
rather than getIo
.
老问题,但它让我看向正确的方向。 selfLink是元数据请求的链接,您需要mediaLink来获取实际的对象数据,而它是getAuth而不是getIo。
This script will output the file contents (given you have already initialised a $client
object) :
此脚本将输出文件内容(假设您已初始化$ client对象):
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
$request = new Google_Http_Request($object->getMediaLink());
$response = $client->getAuth()->authenticatedRequest($request);
echo $response->getResponseBody();
#2
0
This is not valid for apiclient ~2.0, see UPGRADING.md file in github.
这对于apiclient~2.0无效,请参阅github中的UPGRADING.md文件。
Working code with apiclient ~2.0 should be:
使用apiclient~2.0的工作代码应该是:
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
// create an authorized HTTP client
$httpClient = $client->authorize();
$response = $httpClient->request('GET', $object->getMediaLink());
echo $response->getBody();
or authorizing an existing Guzzle client:
或授权现有的Guzzle客户:
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
// add authorization to an existing client
$httpClient = new GuzzleHttp\Client();
$httpClient = $client->authorize($httpClient);
$response = $httpClient->request('GET', $object->getMediaLink());
echo $response->getBody();
#3
-2
for downloading file, you need grant READER access to allUsers (you can do it from google web console or use google php api)
对于下载文件,您需要授予所有用户的READER访问权限(您可以从谷歌网络控制台或使用谷歌php api)
#1
4
Old question, but it got me looking in the right direction. selfLink
is the link to the metadata request, you need mediaLink
to get the actual object data, and it's getAuth
rather than getIo
.
老问题,但它让我看向正确的方向。 selfLink是元数据请求的链接,您需要mediaLink来获取实际的对象数据,而它是getAuth而不是getIo。
This script will output the file contents (given you have already initialised a $client
object) :
此脚本将输出文件内容(假设您已初始化$ client对象):
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
$request = new Google_Http_Request($object->getMediaLink());
$response = $client->getAuth()->authenticatedRequest($request);
echo $response->getResponseBody();
#2
0
This is not valid for apiclient ~2.0, see UPGRADING.md file in github.
这对于apiclient~2.0无效,请参阅github中的UPGRADING.md文件。
Working code with apiclient ~2.0 should be:
使用apiclient~2.0的工作代码应该是:
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
// create an authorized HTTP client
$httpClient = $client->authorize();
$response = $httpClient->request('GET', $object->getMediaLink());
echo $response->getBody();
or authorizing an existing Guzzle client:
或授权现有的Guzzle客户:
$service = new Google_Service_Storage($client);
$object = $service->objects->get('bucketname', 'objectname');
// add authorization to an existing client
$httpClient = new GuzzleHttp\Client();
$httpClient = $client->authorize($httpClient);
$response = $httpClient->request('GET', $object->getMediaLink());
echo $response->getBody();
#3
-2
for downloading file, you need grant READER access to allUsers (you can do it from google web console or use google php api)
对于下载文件,您需要授予所有用户的READER访问权限(您可以从谷歌网络控制台或使用谷歌php api)