Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Storage): support object retention lock #6829

Merged
merged 8 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Storage/src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ public function exists(array $options = [])
* Acceptable values include, `"authenticatedRead"`,
* `"bucketOwnerFullControl"`, `"bucketOwnerRead"`, `"private"`,
* `"projectPrivate"`, and `"publicRead"`.
* @type array $retention Object retention configuration.
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* @type string $retention.retainUntilTime The earliest time in RFC3339
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* UTC "Zulu" format that the object can be deleted or replaced.
* This is the retention configuration set for this object.
* @type string $retention.mode The mode of the retention configuration,
* which can be either `"Unlocked"` or `"Locked"`.
* @type string $retentionExpirationTime The earliest time in
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* RFC3339 UTC "Zulu" format that the object can be deleted or
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* replaced. This depends on any retention configuration set for
* the object and any retention policy set for the bucket that
* contains the object. This value should normally only be set by
* the back-end API.
* @type array $metadata The full list of available options are outlined
* at the [JSON API docs](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request-body).
* @type array $metadata.metadata User-provided metadata, in key/value pairs.
Expand Down
4 changes: 4 additions & 0 deletions Storage/src/Connection/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ private function resolveUploadOptions(array $args)
}

$args['metadata']['name'] = $args['name'];
if (isset($args['retention'])) {
$args['metadata']['retention'] = $args['retention'];
yash30201 marked this conversation as resolved.
Show resolved Hide resolved
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
}
unset($args['name']);
unset($args['retention']);
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
$args['contentType'] = $args['metadata']['contentType']
?? MimeType::fromFilename($args['metadata']['name']);

Expand Down
4 changes: 4 additions & 0 deletions Storage/src/StorageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ function (array $bucket) use ($userProject) {
* `"projectPrivate"`, and `"publicRead"`.
* @type string $predefinedDefaultObjectAcl Apply a predefined set of
* default object access controls to this bucket.
* @type bool $enableObjectRetention Whether object retention should
* be enabled on this bucket. For more information, refer to the
* [Object Retention Lock](https://cloud.google.com/storage/docs/object-lock)
* documentation.
* @type string $projection Determines which properties to return. May
* be either `"full"` or `"noAcl"`. **Defaults to** `"noAcl"`,
* unless the bucket resource specifies acl or defaultObjectAcl
Expand Down
17 changes: 17 additions & 0 deletions Storage/src/StorageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ public function delete(array $options = [])
* Acceptable values include, `"authenticatedRead"`,
* `"bucketOwnerFullControl"`, `"bucketOwnerRead"`, `"private"`,
* `"projectPrivate"`, and `"publicRead"`.
* @type array $retention Object retention configuration.
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* @type string $retention.retainUntilTime The earliest time in RFC3339
* UTC "Zulu" format that the object can be deleted or replaced.
* This is the retention configuration set for this object.
* @type string $retention.mode The mode of the retention configuration,
* which can be either `"Unlocked"` or `"Locked"`.
* @type string $retentionExpirationTime The earliest time in
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
* RFC3339 UTC "Zulu" format that the object can be deleted or
* replaced. This depends on any retention configuration set for
* the object and any retention policy set for the bucket that
* contains the object. This value should normally only be set by
* the back-end API.
* @type bool $overrideUnlockedRetention Applicable for objects that
* have an unlocked retention configuration. Required to be set to
* `true` if the operation includes a retention property that
* changes the mode to `Locked`, reduces the `retainUntilTime`, or
* removes the retention configuration from the object.
* @type string $projection Determines which properties to return. May
* be either 'full' or 'noAcl'.
* @type string $fields Selector which will cause the response to only
Expand Down
30 changes: 30 additions & 0 deletions Storage/tests/System/ManageObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ public function testListsObjectsWithMatchGlob()
}
}

public function testObjectRetentionLock()
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
{
// Test bucket created with object retention enabled
$bucket = self::createBucket(self::$client, uniqid('object_retention-'), [
'enableObjectRetention' => true
]);
$this->assertEquals($bucket->info()['objectRetention']['mode'], 'Enabled');

// Test create object with object retention enabled
$objectName = "object-retention-lock";
$expires = (new \DateTime)->add(
\DateInterval::createFromDateString('+2 hours')
);
$object = $bucket->upload(self::DATA, [
'name' => $objectName,
'retention' => [
'mode' => 'Unlocked',
'retainUntilTime' => $expires->format(\DateTime::RFC3339)
]
]);
$this->assertEquals($object->info()['retention']['mode'], 'Unlocked');

// Test patch object to disable object retention
$object->update([
'retention' => [],
'overrideUnlockedRetention' => true
]);
$this->assertNotContains('retention', $object->info());
}

public function testObjectExists()
{
$object = self::$bucket->upload(self::DATA, ['name' => uniqid(self::TESTING_PREFIX)]);
Expand Down