Skip to content

Instantly share code, notes, and snippets.

@stetic
Last active September 25, 2023 19:20
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save stetic/c97c94a10f9c6b591883 to your computer and use it in GitHub Desktop.
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
<?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 = "1234-xyz@developer.gserviceaccount.com";
$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();
}
@fornve
Copy link

fornve commented Jan 12, 2016

Lifesaver! This is only one place where you can find how to get/read non-public file from google cloud storage using php.

@AndroAndrei
Copy link

excelent info thanks

@chrispappas
Copy link

Exactly what I was looking for. Will be sharing!

@dheeraj008
Copy link

Where should this code be deployed?? I'm not able to run it on lampserver

@jordipolo87
Copy link

Thank you for the sample, it works like a charm! ;)

@vtedesco
Copy link

vtedesco commented Nov 1, 2017

Thanks for sharing this !

@Markmwaura
Copy link

You, my friend, are a LifeSaver.Thanks

@shawkatvirtuos
Copy link

how to set destination ?

@jeansouzak
Copy link

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