Downloading files using google-api-php-client

让人想犯罪 __ 提交于 2019-12-09 12:55:41

问题


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/

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

$request = new Google_HttpRequest($object['selfLink']);
$response = $this->client->getIo()->authenticatedRequest($request);

回答1:


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.

This script will output the file contents (given you have already initialised a $client object) :

$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:


This is not valid for apiclient ~2.0, see UPGRADING.md file in github.

Working code with apiclient ~2.0 should be:

$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:

$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:


for downloading file, you need grant READER access to allUsers (you can do it from google web console or use google php api)



来源:https://stackoverflow.com/questions/17855705/downloading-files-using-google-api-php-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!