-
-
Save AidanThreadgold/9997654 to your computer and use it in GitHub Desktop.
| <?php | |
| require_once("src/Google/Client.php"); | |
| require_once("src/Google/Service/Storage.php"); | |
| /** | |
| * Connect to Google Cloud Storage API | |
| */ | |
| $client = new Google_Client(); | |
| $client->setApplicationName("App Name"); | |
| // $stored_access_token - your cached oauth access token | |
| if( $stored_access_token ) { | |
| $client->setAccessToken( $stored_access_token ); | |
| } | |
| $credential = new Google_Auth_AssertionCredentials( | |
| "service account email address", | |
| ['https://www.googleapis.com/auth/devstorage.read_write'], | |
| file_get_contents("path/to/private-key-certificate.p12") | |
| ); | |
| $client->setAssertionCredentials($credential); | |
| if($client->getAuth()->isAccessTokenExpired()) { | |
| $client->getAuth()->refreshTokenWithAssertion($credential); | |
| // Cache the access token however you choose, getting the access token with $client->getAccessToken() | |
| } | |
| /** | |
| * Upload a file to google cloud storage | |
| */ | |
| $storage = new Google_Service_Storage($client); | |
| $file_name = "what you want your file to be named on Google Cloud Storage"; | |
| $obj = new Google_Service_Storage_StorageObject(); | |
| $obj->setName($file_name); | |
| $storage->objects->insert( | |
| "bucket name", | |
| $obj, | |
| ['name' => $file_name, 'data' => file_get_contents("local path of the file to upload"), 'uploadType' => 'media'] | |
| ); |
@markbratanov To upload into subfolder
$file_name = 'sub_bucket/subbucket/file.png';@noxtras
For publicly shared objects, you need something like this:
// this access control is for project owners
$ownerAccess = new \Google_Service_Storage_ObjectAccessControl();
$ownerAccess->setEntity('project-owners-' . $projectId);
$ownerAccess->setRole('OWNER');
// this access control is for public access
$readerAccess = new \Google_Service_Storage_ObjectAccessControl();
$readerAccess->setEntity('allUsers');
$readerAccess->setRole('READER');
$obj = new \Google_Service_Storage_StorageObject();
$obj->setName($filename);
$obj->setAcl([$ownerAccess, $readerAccess]);
$storage->objects->insert(
'my_bucket',
$obj,
[
'data' => file_get_contents($filename),
'uploadType' => 'media',
]
);Hi, thanks!
Hi, thx so much for this code snippet. Works perfectly :) 👍
I've only got one additional question. How can I add metadata with this script? Because if I upload for example a image file the standard Content-Type is always application/octet-stream which leasds to direct download on opening it via url. So I want to set it to for example image/jpeg because then I can view the file over URL and force download via readfile()if necessary.
@the0ne25
You could specify the mimeType in opt params like this:
$storage->objects->insert(
'my_bucket',
$obj,
[
'data' => file_get_contents($filename),
'uploadType' => 'media',
'mimeType' => 'image/jpeg'
]
);
Thank you for an example.
This example works great, thanks! The simplest way to make the upload publicly shared is:
$storage->objects->insert(
'my_bucket',
$obj,
[
'data' => file_get_contents($filename),
'uploadType' => 'media',
'mimeType' => 'image/jpeg',
'predefinedAcl' => 'publicRead' // THIS LINE OF CODE MAKES FILE PUBLIC FOR READING
]
);
Thanks for this!
Did you have to include the entire Google cloud PHP API library? Or just the two files Client.php and Storage.php? Can I somehow just require those two files?
For this library : https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-storage-ga
Use as following :
$bucket->upload(
fopen('data3.txt', 'r'), ['predefinedAcl' => 'publicRead']
);
how do i stored the link in db.. to load my contents
Thx it works!