sparqapi/api/application/libraries/Googlecloud.php
2023-02-28 16:31:26 +05:30

321 lines
9.0 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Google Cloud Platform
*
* @author Velz
*
*
* Description : To upload images and files.
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once APPPATH.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class Googlecloud
{
private $googleClient;
private $CI;
public function __construct()
{
// 'keyFilePath' => APPPATH.'/client_secret_storage_389134811365-cnd4r6014l500bfpskt7ahs2apvcu3mm.apps.googleusercontent.com.json'
$this->CI =& get_instance();
$this->googleClient = new StorageClient([
'keyFilePath' => $_SERVER['DOCUMENT_ROOT'].'/env/cet_gcp_credential.json'
]);
// Authenticate using keyfile data
// $this->googleClient = new StorageClient([
// 'keyFile' => json_decode(file_get_contents(APPPATH.'/client_secret_storage_389134811365-cnd4r6014l500bfpskt7ahs2apvcu3mm.apps.googleusercontent.com.json'), true)
// ]);
}
/**
 * List all Cloud Storage buckets for the current project.
* Refer : get_bucket_metadata() in GCP Docs
 */
public function listAllBuckets()
{
// $storage = new StorageClient();
$storage = $this->googleClient;
$array = array();
try{$buckets = $storage->buckets();
foreach ($storage->buckets() as $bucket) {
// printf('Bucket: %s' . PHP_EOL, $bucket->name());
array_push($array, $bucket->name());
}
return $array;
}
catch (Exception $ex) {
return $ex->getMessage() . "\n<pre>";
print_r($ex->getTraceAsString());
"</pre>";
}
}
/**
 * Create a new bucket with a custom default storage class and location.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
* Refer : create_bucket_class_location() in GCP Docs
 */
function createNewBucket(string $bucketName): string
{
// $storage = new StorageClient();
try {
$storage = $this->googleClient;
$storageClass = 'COLDLINE';
$location = 'ASIA';
$bucket = $storage->createBucket($bucketName, [
'storageClass' => $storageClass,
'location' => $location,
]);
$objects = $bucket->objects([
'encryption' => [ 'defaultKmsKeyName' => null,]
]);
return $bucketName;
// printf('Created bucket %s in %s with storage class %s', $bucketName, $storageClass, $location);
} catch (Exception $ex) {
return $ex->getMessage() . "\n<pre>";
print_r($ex->getTraceAsString());
"</pre>";
}
}
/**
 * Upload a file.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 * @param string $objectName The name of your Cloud Storage object.
 * (e.g. 'my-object')
 * @param string $source The path to the file to upload.
 * (e.g. '/path/to/your/file')
* Refer : upload_object() in GCP Docs
 */
function uploadFileToGCPBucket(string $bucketName, string $objectName, string $source)
{
// $storage = new StorageClient();
$result = '';
try {
$storage = $this->googleClient;
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
// $result = $object;
// print_r($result);
$result = true;
}
catch (Exception $ex) {
$result = false;
}
return $result;
// printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
/**
 * Generate a v4 signed URL for downloading an object.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 * @param string $objectName The name of your Cloud Storage object.
 * (e.g. 'my-object')
* Refer : get_object_v4_signed_url() in GCP Docs
 */
function getSingleObjectInaBucketAsSignedURI(string $bucketName, string $objectName): string
{
try {
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$url = $object->signedUrl(
# This URL is valid for 15 minutes
new \DateTime('15 min'),
[
'version' => 'v4',
]
);
$presignedUrl = (string) $url;
// print('Generated GET signed URL:' . PHP_EOL);
// print($url . PHP_EOL);
// print('You can use this URL with any user agent, for example:' . PHP_EOL);
// print('curl ' . $url . PHP_EOL);
}catch (Exception $ex) {
return $ex->getMessage() . "\n<pre>";
print_r($ex->getTraceAsString());
"</pre>";
}
return $presignedUrl;
}
/**
 * List Cloud Storage bucket objects.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
* Refer : list_objects() in GCP Docs
 */
function getAllObjectsInaBucket(string $bucketName)
{
// $storage = new StorageClient();
$objects = array();
try {
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
foreach ($bucket->objects() as $object) {
// printf('Object: %s' . PHP_EOL, $object->name());
array_push($objects, $object->name());
}
} catch (Exception $ex) {
return $ex->getMessage() . "\n<pre>";
print_r($ex->getTraceAsString());
"</pre>";
}
return $objects;
}
/**
 * Delete an object.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 * @param string $objectName The name of your Cloud Storage object.
 * (e.g. 'my-object')
* Refer : delete_object() in GCP Docs
 */
function deleteSingleObjFromBucket(string $bucketName, string $objectName): string
{
// $storage = new StorageClient();
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->delete();
// printf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
return 'Deleted gs://%s/%s' . PHP_EOL.$objectName;
}
/**
 * Change the default storage class for the given bucket.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 */
function change_default_storage_class(string $bucketName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$storageClass = 'COLDLINE';
$bucket->update([
'storageClass' => $storageClass,
]);
printf(
'Default storage class for bucket %s has been set to %s',
$bucketName,
$storageClass
);
}
/**
 * Delete a Cloud Storage Bucket.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
* Refer : delete_bucket() in GCP Docs.
 */
function delete_bucket(string $bucketName): void
{
// $storage = new StorageClient();
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
$bucket->delete();
printf('Bucket deleted: %s' . PHP_EOL, $bucket->name());
}
/**
 * Get bucket metadata.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
* Refer : get_bucket_metadata() in GCP Docs
 */
function get_bucket_metadata(string $bucketName): void
{
// $storage = new StorageClient();
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
$info = $bucket->info();
printf('Bucket Metadata: %s' . PHP_EOL, print_r($info));
}
/**
 * Download an object from Cloud Storage and save it as a local file.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 * @param string $objectName The name of your Cloud Storage object.
 * (e.g. 'my-object')
 * @param string $destination The local destination to save the object.
 * (e.g. '/path/to/your/file')
* Refer : download_object() in GCP Docs
 */
function download_object(string $bucketName, string $objectName, string $destination): void
{
$storage = $this->googleClient;
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->downloadToFile($destination);
printf(
'Downloaded gs://%s/%s to %s' . PHP_EOL,
$bucketName,
$objectName,
basename($destination));
}
/**
 * Copy an object to a new name and/or bucket.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * (e.g. 'my-bucket')
 * @param string $objectName The name of your Cloud Storage object.
 * (e.g. 'my-object')
 * @param string $newBucketName The destination bucket name.
 * (e.g. 'my-other-bucket')
 * @param string $newObjectName The destination object name.
 * (e.g. 'my-other-object')
* Refer : copy_object() in GCP Docs
 */
function copyGCPObject(string $bucketName, string $objectName, string $newBucketName, string $newObjectName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->copy($newBucketName, ['name' => $newObjectName]);
printf('Copied gs://%s/%s to gs://%s/%s' . PHP_EOL,
$bucketName, $objectName, $newBucketName, $newObjectName);
}
}
?>