Last active
September 25, 2023 19:20
-
-
Save stetic/c97c94a10f9c6b591883 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 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(); | |
} | |
Thank you for the sample, it works like a charm! ;)
Thanks for sharing this !
You, my friend, are a LifeSaver.Thanks
how to set destination ?
I wrote a package to download / get local file and upload to Google Cloud Storage, maybe it help as well:
https://github.com/jeansouzak/duf
You can send a local file or external resource:
use JeanSouzaK\Duf\Duff;
use JeanSouzaK\Duf\Prepare\WebResource;
use JeanSouzaK\Duf\Prepare\LocalResource;
$duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [
'project_id' => 'storage-test-227305',
'key_path' => '/home/env/storage-test-227305-8472347a39489.json'
]);
// - Chaining example -
$uploadResults = $duf->prepare([
new LocalResource('imagem', '/home/test/images/test.jpg'),
new WebResource('teste.png', 'https://dummyimage.com/600x400/000/fff')
])->download()->addBucket('your-bucket-name')->upload();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where should this code be deployed?? I'm not able to run it on lampserver