Last active
January 25, 2021 14:07
-
-
Save hassanrazadev/d138d00a44be732f056173494895c03a to your computer and use it in GitHub Desktop.
GoogleCloudStorage helper file for PHP/Larave
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 | |
namespace App\Utils; | |
use Exception; | |
use Google\Cloud\Storage\StorageClient; | |
class GoogleCloudStorage{ | |
/** | |
* @var string | |
* private key generated from google console | |
*/ | |
private $privateKeyFileContent; | |
/** | |
* GoogleCloudRequest constructor. | |
*/ | |
public function __construct(){ | |
$this->privateKeyFileContent = '{ | |
"type": "service_account", | |
"project_id": "", | |
"private_key_id": "", | |
"private_key": "-----BEGIN PRIVATE KEY----- PRIVATE KEY -----END PRIVATE KEY-----\n", | |
"client_email": "", | |
"client_id": "", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "" | |
}'; | |
} | |
/* | |
* NOTE: if the server is a shared hosting by third party company then private key should not be stored as a file, | |
* may be better to encrypt the private key value then store the 'encrypted private key' value as string in database, | |
* so every time before use the private key we can get a user-input (from UI) to get password to decrypt it. | |
*/ | |
public function uploadFile($bucketName, $fileContent, $cloudPath) | |
{ | |
// connect to Google Cloud Storage using private key as authentication | |
try { | |
$storage = new StorageClient([ | |
'keyFile' => json_decode($this->privateKeyFileContent, true) | |
]); | |
} catch (Exception $e) { | |
// maybe invalid private key ? | |
print $e; | |
return false; | |
} | |
// set which bucket to work in | |
$bucket = $storage->bucket($bucketName); | |
// upload/replace file | |
$storageObject = $bucket->upload( | |
$fileContent, | |
['name' => $cloudPath] | |
// if $cloudPath is existed then will be overwrite without confirmation | |
// NOTE: | |
// a. do not put prefix '/', '/' is a separate folder name !! | |
// b. private key MUST have 'storage.objects.delete' permission if want to replace file ! | |
); | |
// is it succeed ? | |
return $storageObject != null; | |
} | |
/** | |
* @param $bucketName | |
* @param $cloudPath | |
* @return array|bool | |
* this (listFiles) method not used in this example but you may use according to your need | |
*/ | |
public function getFileInfo($bucketName, $cloudPath) | |
{ | |
// connect to Google Cloud Storage using private key as authentication | |
try { | |
$storage = new StorageClient([ | |
'keyFile' => json_decode($this->privateKeyFileContent, true) | |
]); | |
} catch (Exception $e) { | |
// maybe invalid private key ? | |
print $e; | |
return false; | |
} | |
// set which bucket to work in | |
$bucket = $storage->bucket($bucketName); | |
$object = $bucket->object($cloudPath); | |
return $object->info(); | |
} | |
public function listFiles($bucket, $directory = null) | |
{ | |
if ($directory == null) { | |
// list all files | |
$objects = $bucket->objects(); | |
} else { | |
// list all files within a directory (sub-directory) | |
$options = array('prefix' => $directory); | |
$objects = $bucket->objects($options); | |
} | |
foreach ($objects as $object) { | |
print $object->name() . PHP_EOL; | |
// NOTE: if $object->name() ends with '/' then it is a 'folder' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment