From 8dded4e8e40ef7414d6bc467f91230f2fd87bca5 Mon Sep 17 00:00:00 2001 From: Ra'Jiska Date: Thu, 6 Oct 2022 12:19:06 +0800 Subject: [PATCH 1/2] GCS Support for Uniform ACL Buckets --- cfg/conf.sample.php | 1 + lib/Configuration.php | 5 +++-- lib/Data/GoogleCloudStorage.php | 37 ++++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cfg/conf.sample.php b/cfg/conf.sample.php index 3c7f462..ab37da7 100644 --- a/cfg/conf.sample.php +++ b/cfg/conf.sample.php @@ -175,6 +175,7 @@ dir = PATH "data" ;[model_options] ;bucket = "my-private-bin" ;prefix = "pastes" +;uniformacl = false ;[model] ; example of DB configuration for MySQL diff --git a/lib/Configuration.php b/lib/Configuration.php index 56e55ad..9f4e35f 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -153,8 +153,9 @@ class Configuration ) ) { $values = array( - 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null, - 'prefix' => 'pastes', + 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null, + 'prefix' => 'pastes', + 'uniformacl' => false, ); } diff --git a/lib/Data/GoogleCloudStorage.php b/lib/Data/GoogleCloudStorage.php index 2e8e2c5..1f11a6e 100644 --- a/lib/Data/GoogleCloudStorage.php +++ b/lib/Data/GoogleCloudStorage.php @@ -37,6 +37,15 @@ class GoogleCloudStorage extends AbstractData */ private static $_prefix = 'pastes'; + /** + * bucket acl type + * + * @access private + * @static + * @var bool + */ + private static $_uniformacl = false; + /** * returns a Google Cloud Storage data backend. * @@ -62,6 +71,9 @@ class GoogleCloudStorage extends AbstractData if (is_array($options) && array_key_exists('prefix', $options)) { self::$_prefix = $options['prefix']; } + if (is_array($options) && array_key_exists('uniformacl', $options)) { + self::$_uniformacl = $options['uniformacl']; + } if (empty(self::$_client)) { self::$_client = class_exists('StorageClientStub', false) ? @@ -100,21 +112,19 @@ class GoogleCloudStorage extends AbstractData */ 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 { - self::$_bucket->upload(Json::encode($payload), array( + $data = array( 'name' => $key, 'chunkSize' => 262144, - 'predefinedAcl' => 'private', 'metadata' => array( 'content-type' => 'application/json', - 'metadata' => $metadata, + 'metadata' => $payload, ), - )); + ); + if (!self::$_uniformacl) { + $data['predefinedAcl'] = 'private'; + } + self::$_bucket->upload(Json::encode($payload), $data); } catch (Exception $e) { error_log('failed to upload ' . $key . ' to ' . self::$_bucket->name() . ', ' . trim(preg_replace('/\s\s+/', ' ', $e->getMessage()))); @@ -277,15 +287,18 @@ class GoogleCloudStorage extends AbstractData $metadata['value'] = strval($value); } try { - self::$_bucket->upload($value, array( + $data = array( 'name' => $key, 'chunkSize' => 262144, - 'predefinedAcl' => 'private', 'metadata' => array( 'content-type' => 'application/json', 'metadata' => $metadata, ), - )); + ); + if (!self::$_uniformacl) { + $data['predefinedAcl'] = 'private'; + } + self::$_bucket->upload($value, $data); } catch (Exception $e) { error_log('failed to set key ' . $key . ' to ' . self::$_bucket->name() . ', ' . trim(preg_replace('/\s\s+/', ' ', $e->getMessage()))); From 8dbe60621de10d36befb3a3dda859dab31ede5f2 Mon Sep 17 00:00:00 2001 From: Ra'Jiska Date: Thu, 6 Oct 2022 14:41:37 +0800 Subject: [PATCH 2/2] Fix GCS Upload Metadata Mistake --- lib/Data/GoogleCloudStorage.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/Data/GoogleCloudStorage.php b/lib/Data/GoogleCloudStorage.php index 1f11a6e..e876325 100644 --- a/lib/Data/GoogleCloudStorage.php +++ b/lib/Data/GoogleCloudStorage.php @@ -112,13 +112,18 @@ class GoogleCloudStorage extends AbstractData */ 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 { $data = array( 'name' => $key, 'chunkSize' => 262144, 'metadata' => array( 'content-type' => 'application/json', - 'metadata' => $payload, + 'metadata' => $metadata, ), ); if (!self::$_uniformacl) {