353 lines
13 KiB
PHP
353 lines
13 KiB
PHP
<?php
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
# Imports the Google Cloud client library
|
|
use Google\Cloud\Storage\StorageClient;
|
|
|
|
class GoogleStorage {
|
|
|
|
private $storage;
|
|
private $projectId;
|
|
private $authFile;
|
|
public $prefix = 'adminsavvy';
|
|
|
|
public function __construct() {
|
|
}
|
|
|
|
public function Init($projectId, $authFile) {
|
|
// Your Google Cloud Platform project ID
|
|
$this->projectId = $projectId; // 'float-app-224118';
|
|
// The file path to credentials JSON
|
|
$this->authFile = $authFile; // './float-app-224118-52ef1783d2c5.json';
|
|
// Instantiates a client
|
|
$this->storage = new StorageClient([
|
|
'projectId' => $projectId,
|
|
'keyFile' => json_decode(file_get_contents($authFile), true)
|
|
]);
|
|
}
|
|
/*
|
|
* @type array $keyFile The contents of the service account credentials
|
|
* .json file retrieved from the Google Developer's Console.
|
|
* Ex: `json_decode(file_get_contents($path), true)`.
|
|
* @type string $keyFilePath The full path to your service account
|
|
* credentials .json file retrieved from the Google Developers
|
|
* Console.
|
|
*/
|
|
|
|
/**
|
|
* Create a Cloud Storage Bucket.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName name of the bucket to create.
|
|
* @param string $options options for the new bucket.
|
|
*
|
|
* @return Google\Cloud\Storage\Bucket the newly created bucket.
|
|
*/
|
|
public function CreateBucket($bucketName) {
|
|
// Creates the new bucket
|
|
$bucket = $this->storage->createBucket($bucketName);
|
|
//echo 'Bucket ' . $bucket->name() . ' created.';
|
|
return $bucket;
|
|
}
|
|
|
|
/**
|
|
* Delete a Cloud Storage Bucket.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of the bucket to delete.
|
|
*
|
|
* @return void
|
|
*/
|
|
function DeleteBucket($bucketName)
|
|
{
|
|
$this->storage = new StorageClient();
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$bucket->delete();
|
|
printf('Bucket deleted: %s' . PHP_EOL, $bucket->name());
|
|
}
|
|
|
|
/**
|
|
* Upload a file.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Google Cloud bucket.
|
|
* @param string $objectName the name of the object.
|
|
* @param string $source the path to the file to upload.
|
|
*
|
|
* @return Psr\Http\Message\StreamInterface
|
|
*/
|
|
function UploadObject($bucketName, $objectName, $source)
|
|
{
|
|
$file = fopen($source, 'r');
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->upload($file, [
|
|
'name' => $objectName
|
|
]);
|
|
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
|
|
return $object;
|
|
}
|
|
|
|
/**
|
|
* Download an object from Cloud Storage and return it as a BLOB.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Google Cloud bucket.
|
|
* @param string $objectName the name of your Google Cloud object.
|
|
*
|
|
* @return BLOB
|
|
*/
|
|
function DownloadObject($bucketName, $objectName)
|
|
{
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->object($objectName);
|
|
$stream = $object->downloadAsStream();
|
|
return $stream->getContents();
|
|
}
|
|
|
|
/**
|
|
* Delete an object.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Cloud Storage bucket.
|
|
* @param string $objectName the name of your Cloud Storage object.
|
|
* @param array $options
|
|
*
|
|
* @return void
|
|
*/
|
|
function DeleteObject($bucketName, $objectName, $options = [])
|
|
{
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->object($objectName);
|
|
$object->delete();
|
|
$msg = sprintf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
|
|
echo $msg;
|
|
return $msg;
|
|
}
|
|
|
|
/**
|
|
* Make an object publically accessible.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Cloud Storage bucket.
|
|
* @param string $objectName the name of your Cloud Storage object.
|
|
*
|
|
* @return void
|
|
*/
|
|
function MakePublic($bucketName, $objectName)
|
|
{
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->object($objectName);
|
|
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
|
|
printf('gs://%s/%s is now public' . PHP_EOL, $bucketName, $objectName);
|
|
}
|
|
|
|
/**
|
|
* List object metadata.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Cloud Storage bucket.
|
|
* @param string $objectName the name of your Cloud Storage object.
|
|
*
|
|
* @return void
|
|
*/
|
|
function ObjectMetadata($bucketName, $objectName)
|
|
{
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->object($objectName);
|
|
$info = $object->info();
|
|
printf('Blob: %s' . PHP_EOL, $info['name']);
|
|
printf('Bucket: %s' . PHP_EOL, $info['bucket']);
|
|
printf('Storage class: %s' . PHP_EOL, $info['storageClass']);
|
|
printf('ID: %s' . PHP_EOL, $info['id']);
|
|
printf('Size: %s' . PHP_EOL, $info['size']);
|
|
printf('Updated: %s' . PHP_EOL, $info['updated']);
|
|
printf('Generation: %s' . PHP_EOL, $info['generation']);
|
|
printf('Metageneration: %s' . PHP_EOL, $info['metageneration']);
|
|
printf('Etag: %s' . PHP_EOL, $info['etag']);
|
|
printf('Crc32c: %s' . PHP_EOL, $info['crc32c']);
|
|
printf('MD5 Hash: %s' . PHP_EOL, $info['md5Hash']);
|
|
printf('Content-type: %s' . PHP_EOL, $info['contentType']);
|
|
printf("Temporary hold: " . (isset($info['temporaryHold']) ? "enabled" : "disabled") . PHP_EOL);
|
|
printf("Event-based hold: " . (isset($info['eventBasedHold']) ? "enabled" : "disabled") . PHP_EOL);
|
|
if (isset($info['retentionExpirationTime'])) {
|
|
printf("retentionExpirationTime: " . $info['retentionExpirationTime'] . PHP_EOL);
|
|
}
|
|
if (isset($info['metadata'])) {
|
|
printf('Metadata: %s', print_r($info['metadata'], true));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* List all Cloud Storage buckets for the current project.
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @return void
|
|
*/
|
|
function ListBuckets()
|
|
{
|
|
$buckets = array();
|
|
foreach ($this->storage->buckets() as $bucket) {
|
|
//printf('Bucket: %s' . PHP_EOL, $bucket->name());
|
|
$buckets[$bucket->name()] = $bucket;
|
|
}
|
|
return $buckets;
|
|
}
|
|
|
|
/**
|
|
* List Cloud Storage bucket objects.
|
|
* https://cloud.google.com/storage/docs/listing-objects
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Cloud Storage bucket.
|
|
*
|
|
* @return void
|
|
*/
|
|
function ListObjects($bucketName)
|
|
{
|
|
$objects = array();
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
foreach ($bucket->objects() as $object) {
|
|
//printf('Object: %s' . PHP_EOL, $object->name());
|
|
$objects[$object->name()] = $object;
|
|
}
|
|
return $objects;
|
|
}
|
|
|
|
/**
|
|
* List Cloud Storage bucket objects.
|
|
* https://cloud.google.com/storage/docs/listing-objects
|
|
*
|
|
* @param Google\Cloud\Storage\StorageClient $storage Google storage service client
|
|
* @param string $bucketName the name of your Cloud Storage bucket.
|
|
* @param string $objectName the name of your Cloud Storage object.
|
|
* @param string $responseType Response content-type for the object URL
|
|
* @param integer $duration Number of seconds before the signature expires
|
|
* @param string $method Optional HTTP verb for URL
|
|
*
|
|
* @return string Signed google storage URL
|
|
*/
|
|
function SignedUrl($bucketName, $objectName, $responseType, $duration = 60, $method = 'GET')
|
|
{
|
|
$bucket = $this->storage->bucket($bucketName);
|
|
$object = $bucket->object($objectName);
|
|
$url = $object->signedUrl(
|
|
new \DateTime('+ ' . $duration . ' seconds'),
|
|
array(
|
|
'method' => $method,
|
|
'responseType' => $responseType,
|
|
'allowPost' => $method=='POST'
|
|
));
|
|
return $url;
|
|
}
|
|
/*
|
|
'method' => 'GET',
|
|
'cname' => self::DEFAULT_DOWNLOAD_URL,
|
|
'contentMd5' => null,
|
|
'contentType' => null,
|
|
'headers' => [],
|
|
'saveAsName' => null,
|
|
'responseDisposition' => null,
|
|
'responseType' => null,
|
|
'keyFile' => null,
|
|
'keyFilePath' => null,
|
|
'allowPost' => false,
|
|
'forceOpenssl' => false
|
|
*/
|
|
private function BaseEncode($num, $alphabet) {
|
|
$base_count = strlen($alphabet);
|
|
$encoded = '';
|
|
while ($num >= $base_count) {
|
|
$div = $num/$base_count;
|
|
$mod = ($num-($base_count*intval($div)));
|
|
if ($mod>=0 && $mod<$base_count) {
|
|
$encoded = $alphabet[$mod] . $encoded;
|
|
}
|
|
$num = intval($div);
|
|
}
|
|
if ($num) $encoded = $alphabet[$num] . $encoded;
|
|
return $encoded;
|
|
}
|
|
|
|
private function BaseDecode($num, $alphabet) {
|
|
$decoded = 0;
|
|
$multi = 1;
|
|
while (strlen($num) > 0) {
|
|
$digit = $num[strlen($num)-1];
|
|
$decoded += $multi * strpos($alphabet, $digit);
|
|
$multi = $multi * strlen($alphabet);
|
|
$num = substr($num, 0, -1);
|
|
}
|
|
return $decoded;
|
|
}
|
|
|
|
public function UniqueId() {
|
|
// 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ
|
|
$alphabet = "pRFsNvKifkJtA5Umy9QSD8PTYcZH23dCeWg6aqVXwnr7jxuGMLhE4zBob1";
|
|
return $this->BaseEncode(
|
|
filter_var(microtime(),FILTER_SANITIZE_NUMBER_INT),
|
|
$alphabet);
|
|
}
|
|
|
|
}
|
|
|
|
//*
|
|
// run with GOOGLE_STORAGE_TEST=1 php -f GoogleStorage.php
|
|
if (isset($_SERVER['GOOGLE_STORAGE_TEST']) && $_SERVER['GOOGLE_STORAGE_TEST']==1) {
|
|
// Create bucket 'adminsavvy-card-images' and list buckets
|
|
//TestGoogleStorage(1,1,0,0,0,0,0,0);
|
|
// Upload 'singapore.jpg' file to 'adminsavvy-card-images' bucket and make the object public
|
|
//TestGoogleStorage(0,0,1,1,0,0,0,0);
|
|
// Download 'singapore.jpg' object from 'adminsavvy-card-images' bucket
|
|
//TestGoogleStorage(0,0,0,0,0,0,1,0);
|
|
// List buckets, List objects in 'adminsavvy-card-images' bucket, get 'singapore.jpg' object metadata, get signed URL
|
|
TestGoogleStorage(0,1,0,0,1,1,0,1);
|
|
}
|
|
//*/
|
|
/*
|
|
* Run the test suite for above defined function
|
|
*
|
|
* @param integer $testCreateBucket 1 to run the create bucket test
|
|
* @param integer $testListBuckets 1 to run the list buckets test
|
|
* @param integer $testUploadObject 1 to run the upload object test
|
|
* @param integer $testMakePublic 1 to run the make public test
|
|
* @param integer $testListObjects 1 to run the list objects test
|
|
* @param integer $testObjectMetadata 1 to run the object metadata test
|
|
* @param integer $testDownloadObject 1 to run the download object test
|
|
* @param integer $testSignedUrl 1 to run the signed URL test
|
|
*
|
|
* @return void
|
|
*/
|
|
function TestGoogleStorage(
|
|
$testCreateBucket,
|
|
$testListBuckets,
|
|
$testUploadObject,
|
|
$testMakePublic,
|
|
$testListObjects,
|
|
$testObjectMetadata,
|
|
$testDownloadObject,
|
|
$testSignedUrl) {
|
|
// Your Google Cloud Platform project ID
|
|
$projectId = 'float-app-224118';
|
|
// The name for the new bucket
|
|
$bucketName = 'adminsavvy-card-images';
|
|
// The file path to upload
|
|
$filePath = '/home/savvy/savvy/savvyoauth2/public/test/singapore.jpg';
|
|
// The file path to credentials JSON
|
|
$authFile = '/home/savvy/savvy/savvyoauth2/public/include/float-app-224118-52ef1783d2c5.json';
|
|
|
|
// Instantiates a client
|
|
$storage = new GoogleStorage($projectId, $authFile);
|
|
|
|
if ($testCreateBucket) var_dump($storage->CreateBucket($bucketName));
|
|
if ($testListBuckets) $storage->ListBuckets();
|
|
if ($testUploadObject) var_dump($storage->UploadObject($bucketName, basename($filePath), $filePath));
|
|
if ($testMakePublic) $storage->MakePublic($bucketName, basename($filePath));
|
|
if ($testListObjects) $storage->ListObjects($bucketName);
|
|
if ($testObjectMetadata) $storage->ObjectMetadata($bucketName, basename($filePath));
|
|
if ($testDownloadObject) file_put_contents('test.jpg',$storage->DownloadObject($bucketName, basename($filePath)));
|
|
if ($testSignedUrl) var_dump($storage->SignedUrl($bucketName, basename($filePath), mime_content_type($filePath), 864000));
|
|
}
|