GCS Support for Uniform ACL Buckets

This commit is contained in:
Ra'Jiska 2022-10-06 12:19:06 +08:00
parent 160207b25a
commit 8dded4e8e4
3 changed files with 29 additions and 14 deletions

View file

@ -175,6 +175,7 @@ dir = PATH "data"
;[model_options] ;[model_options]
;bucket = "my-private-bin" ;bucket = "my-private-bin"
;prefix = "pastes" ;prefix = "pastes"
;uniformacl = false
;[model] ;[model]
; example of DB configuration for MySQL ; example of DB configuration for MySQL

View file

@ -155,6 +155,7 @@ class Configuration
$values = array( $values = array(
'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null, 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
'prefix' => 'pastes', 'prefix' => 'pastes',
'uniformacl' => false,
); );
} }

View file

@ -37,6 +37,15 @@ class GoogleCloudStorage extends AbstractData
*/ */
private static $_prefix = 'pastes'; private static $_prefix = 'pastes';
/**
* bucket acl type
*
* @access private
* @static
* @var bool
*/
private static $_uniformacl = false;
/** /**
* returns a Google Cloud Storage data backend. * returns a Google Cloud Storage data backend.
* *
@ -62,6 +71,9 @@ class GoogleCloudStorage extends AbstractData
if (is_array($options) && array_key_exists('prefix', $options)) { if (is_array($options) && array_key_exists('prefix', $options)) {
self::$_prefix = $options['prefix']; self::$_prefix = $options['prefix'];
} }
if (is_array($options) && array_key_exists('uniformacl', $options)) {
self::$_uniformacl = $options['uniformacl'];
}
if (empty(self::$_client)) { if (empty(self::$_client)) {
self::$_client = class_exists('StorageClientStub', false) ? self::$_client = class_exists('StorageClientStub', false) ?
@ -100,21 +112,19 @@ class GoogleCloudStorage extends AbstractData
*/ */
private function _upload($key, $payload) private function _upload($key, $payload)
{ {
$metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
foreach ($metadata as $k => $v) {
$metadata[$k] = strval($v);
}
try { try {
self::$_bucket->upload(Json::encode($payload), array( $data = array(
'name' => $key, 'name' => $key,
'chunkSize' => 262144, 'chunkSize' => 262144,
'predefinedAcl' => 'private',
'metadata' => array( 'metadata' => array(
'content-type' => 'application/json', 'content-type' => 'application/json',
'metadata' => $metadata, 'metadata' => $payload,
), ),
)); );
if (!self::$_uniformacl) {
$data['predefinedAcl'] = 'private';
}
self::$_bucket->upload(Json::encode($payload), $data);
} catch (Exception $e) { } catch (Exception $e) {
error_log('failed to upload ' . $key . ' to ' . self::$_bucket->name() . ', ' . error_log('failed to upload ' . $key . ' to ' . self::$_bucket->name() . ', ' .
trim(preg_replace('/\s\s+/', ' ', $e->getMessage()))); trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
@ -277,15 +287,18 @@ class GoogleCloudStorage extends AbstractData
$metadata['value'] = strval($value); $metadata['value'] = strval($value);
} }
try { try {
self::$_bucket->upload($value, array( $data = array(
'name' => $key, 'name' => $key,
'chunkSize' => 262144, 'chunkSize' => 262144,
'predefinedAcl' => 'private',
'metadata' => array( 'metadata' => array(
'content-type' => 'application/json', 'content-type' => 'application/json',
'metadata' => $metadata, 'metadata' => $metadata,
), ),
)); );
if (!self::$_uniformacl) {
$data['predefinedAcl'] = 'private';
}
self::$_bucket->upload($value, $data);
} catch (Exception $e) { } catch (Exception $e) {
error_log('failed to set key ' . $key . ' to ' . self::$_bucket->name() . ', ' . error_log('failed to set key ' . $key . ' to ' . self::$_bucket->name() . ', ' .
trim(preg_replace('/\s\s+/', ' ', $e->getMessage()))); trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));