Skip to content

Commit d4d6115

Browse files
feat(Storage): AbortIncompleteMultipartUpload LifecycleAction (#7099)
1 parent fd396c8 commit d4d6115

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

Storage/src/Lifecycle.php

+73
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,79 @@ public function addDeleteRule(array $condition)
139139
return $this;
140140
}
141141

142+
/**
143+
* Adds a AbortIncompleteMultipartUpload lifecycle rule to Object Lifecycle.
144+
*
145+
* Example:
146+
* ```
147+
* $lifecycle->addAbortIncompleteMultipartUploadRule([
148+
* 'age' => 50,
149+
* 'isLive' => true
150+
* ]);
151+
* ```
152+
*
153+
* @param array $condition {
154+
* The condition(s) where the rule will apply.
155+
*
156+
* @type int $age Age of an object (in days). This condition is
157+
* satisfied when an object reaches the specified age.
158+
* @type \DateTimeInterface|string $createdBefore This condition is
159+
* satisfied when an object is created before midnight of the
160+
* specified date in UTC. If a string is given, it must be a date
161+
* in RFC 3339 format with only the date part (for instance,
162+
* "2013-01-15").
163+
* @type \DateTimeInterface|string $customTimeBefore This condition is
164+
* satisfied when the custom time on an object is before this date
165+
* in UTC. If a string is given, it must be a date in RFC 3339
166+
* format with only the date part (for instance, "2013-01-15").
167+
* @type int $daysSinceCustomTime Number of days elapsed since the
168+
* user-specified timestamp set on an object. The condition is
169+
* satisfied if the days elapsed is at least this number. If no
170+
* custom timestamp is specified on an object, the condition does
171+
* not apply.
172+
* @type int $daysSinceNoncurrentTime Number of days elapsed since the
173+
* noncurrent timestamp of an object. The condition is satisfied
174+
* if the days elapsed is at least this number. This condition is
175+
* relevant only for versioned objects. The value of the field
176+
* must be a nonnegative integer. If it's zero, the object version
177+
* will become eligible for Lifecycle action as soon as it becomes
178+
* noncurrent.
179+
* @type bool $isLive Relevant only for versioned objects. If the value
180+
* is `true`, this condition matches live objects; if the value is
181+
* `false`, it matches archived objects.
182+
* @type string[] $matchesStorageClass Objects having any of the storage
183+
* classes specified by this condition will be matched. Values
184+
* include `"MULTI_REGIONAL"`, `"REGIONAL"`, `"NEARLINE"`,
185+
* `"ARCHIVE"`, `"COLDLINE"`, `"STANDARD"`, and
186+
* `"DURABLE_REDUCED_AVAILABILITY"`.
187+
* @type \DateTimeInterface|string $noncurrentTimeBefore This condition
188+
* is satisfied when the noncurrent time on an object is before
189+
* this timestamp. This condition is relevant only for versioned
190+
* objects. If a string is given, it must be a date in RFC 3339
191+
* format with only the date part (for instance, "2013-01-15").
192+
* @type int $numNewerVersions Relevant only for versioned objects. If
193+
* the value is N, this condition is satisfied when there are at
194+
* least N versions (including the live version) newer than this
195+
* version of the object.
196+
* @type string[] $matchesPrefix Objects having names which start with
197+
* values specified by this condition will be matched.
198+
* @type string[] $matchesSuffix Objects having names which end with
199+
* values specified by this condition will be matched.
200+
* }
201+
* @return Lifecycle
202+
*/
203+
public function addAbortIncompleteMultipartUploadRule(array $condition)
204+
{
205+
$this->lifecycle['rule'][] = [
206+
'action' => [
207+
'type' => 'AbortIncompleteMultipartUpload'
208+
],
209+
'condition' => $this->formatCondition($condition)
210+
];
211+
212+
return $this;
213+
}
214+
142215
/**
143216
* Adds an Object Lifecycle Set Storage Class Rule.
144217
*

Storage/tests/Snippet/LifecycleTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ public function testAddDeleteRule()
9999
);
100100
}
101101

102+
public function testAddAbortIncompleteMultipartUploadRule()
103+
{
104+
$snippet = $this->snippetFromMethod(Lifecycle::class, 'addAbortIncompleteMultipartUploadRule');
105+
$snippet->addLocal('lifecycle', $this->lifecycle);
106+
107+
$returnVal = $snippet->invoke('lifecycle')
108+
->returnVal();
109+
110+
$this->assertInstanceOf(Lifecycle::class, $returnVal);
111+
$this->assertEquals(
112+
[
113+
'rule' => [
114+
[
115+
'action' => [
116+
'type' => 'AbortIncompleteMultipartUpload',
117+
],
118+
'condition' => self::$condition
119+
]
120+
]
121+
],
122+
$returnVal->toArray()
123+
);
124+
}
125+
102126
public function testAddSetStorageClassRule()
103127
{
104128
$snippet = $this->snippetFromMethod(Lifecycle::class, 'addSetStorageClassRule');

Storage/tests/System/ManageBucketsTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ public function testCreateBucketWithLifecycleDeleteRule(array $rule, $isError =
137137
$this->assertEquals($lifecycle->toArray(), $bucket->info()['lifecycle']);
138138
}
139139

140+
/**
141+
* @group storage-bucket-lifecycle
142+
* @dataProvider lifecycleRules
143+
*/
144+
public function testCreateBucketWithLifecycleAbortIncompleteMultipartUploadRule(array $rule, $isError = false)
145+
{
146+
if ($isError) {
147+
$this->expectException(BadRequestException::class);
148+
}
149+
150+
$lifecycle = Bucket::lifecycle();
151+
$lifecycle->addAbortIncompleteMultipartUploadRule($rule);
152+
153+
$bucket = self::createBucket(self::$client, uniqid(self::TESTING_PREFIX), [
154+
'lifecycle' => $lifecycle
155+
]);
156+
157+
$this->assertEquals($lifecycle->toArray(), $bucket->info()['lifecycle']);
158+
}
159+
140160
/**
141161
* @group storage-bucket-lifecycle
142162
* @dataProvider lifecycleRules

Storage/tests/Unit/LifecycleTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public function testAddSetStorageClassRuleDateTimeTransform($rule, \DateTimeInte
160160

161161
$result = $this->lifecycle->toArray();
162162

163+
$this->assertEquals('SetStorageClass', $result['rule'][0]['action']['type']);
164+
163165
$this->assertEquals(
164166
$dateString,
165167
$result['rule'][0]['condition'][$rule]
@@ -186,6 +188,39 @@ public function testAddDeleteRuleDateTimeTransform($rule, \DateTimeInterface $dt
186188

187189
$result = $this->lifecycle->toArray();
188190

191+
$this->assertEquals('Delete', $result['rule'][0]['action']['type']);
192+
193+
$this->assertEquals(
194+
$dateString,
195+
$result['rule'][0]['condition'][$rule]
196+
);
197+
198+
$this->assertEquals(
199+
$dateString,
200+
$result['rule'][1]['condition'][$rule]
201+
);
202+
}
203+
204+
/**
205+
* @dataProvider dateTimeRules
206+
*/
207+
public function testAddAbortIncompleteMultipartUploadRule($rule, \DateTimeInterface $dt, $dateString)
208+
{
209+
$this->lifecycle->addAbortIncompleteMultipartUploadRule([
210+
$rule => $dt
211+
]);
212+
213+
$this->lifecycle->addAbortIncompleteMultipartUploadRule([
214+
$rule => $dateString
215+
]);
216+
217+
$result = $this->lifecycle->toArray();
218+
219+
$this->assertEquals(
220+
'AbortIncompleteMultipartUpload',
221+
$result['rule'][0]['action']['type']
222+
);
223+
189224
$this->assertEquals(
190225
$dateString,
191226
$result['rule'][0]['condition'][$rule]

0 commit comments

Comments
 (0)