Forked from stetic/google-cloud-storage-upload-download.php
Created
December 12, 2015 05:47
-
-
Save henrybear327/10cd9e3310c4c9bb8d98 to your computer and use it in GitHub Desktop.
PHP Example for Google Storage Upload and Download with Google APIs Client Library for PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* PHP Example for Google Storage Up- and Download | |
* with Google APIs Client Library for PHP: | |
* https://github.com/google/google-api-php-client | |
*/ | |
include( "Google/Client.php" ); | |
include( "Google/Service/Storage.php" ); | |
$serviceAccount = "[email protected]"; | |
$key_file = "/path/to/keyfile.p12"; | |
$bucket = "my_bucket"; | |
$file_name = "test.txt"; | |
$file_content = "01101010 01110101 01110011 01110100 00100000 01100001 00100000 01110100 01100101 01110011 01110100"; | |
$auth = new Google_Auth_AssertionCredentials( | |
$serviceAccount, | |
array('https://www.googleapis.com/auth/devstorage.read_write'), | |
file_get_contents($key_file) | |
); | |
$client = new Google_Client(); | |
$client->setAssertionCredentials( $auth ); | |
$storageService = new Google_Service_Storage( $client ); | |
/*** | |
* Write file to Google Storage | |
*/ | |
try | |
{ | |
$postbody = array( | |
'name' => $file_name, | |
'data' => $file_content, | |
'uploadType' => "media" | |
); | |
$gsso = new Google_Service_Storage_StorageObject(); | |
$gsso->setName( $file_name ); | |
$result = $storageService->objects->insert( $bucket, $gsso, $postbody ); | |
print_r($result); | |
} | |
catch (Exception $e) | |
{ | |
print $e->getMessage(); | |
} | |
/*** | |
* Read file from Google Storage | |
*/ | |
try | |
{ | |
$object = $storageService->objects->get( $bucket, $file_name ); | |
$request = new Google_Http_Request($object['mediaLink'], 'GET'); | |
$signed_request = $client->getAuth()->sign($request); | |
$http_request = $client->getIo()->makeRequest($signed_request); | |
echo $http_request->getResponseBody(); | |
} | |
catch (Exception $e) | |
{ | |
print $e->getMessage(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment