Skip to content

Commit 6fec27e

Browse files
authored
feat(storage): support delete source objects on compose (#2224)
* feat(storage): support delete source objects on compose Updates compose file sample to support deleting source objects optionally. Fixes b/441557254 [Generated-by: AI] * Fix trailing whitespaces to resolve styles CI failure Removed trailing whitespaces in storage/test/ObjectsTest.php which caused the styles GitHub Actions check to fail. [Generated-by: AI] * test(storage): use assertTrue and assertFalse instead of assertEquals
1 parent 5cd310f commit 6fec27e

2 files changed

Lines changed: 54 additions & 14 deletions

File tree

‎storage/src/compose_file.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
* (e.g. 'my-object-2')
3838
* @param string $targetObjectName The name of the object to be created.
3939
* (e.g. 'composed-my-object-1-my-object-2')
40+
* @param bool $deleteSourceObjects Whether to delete the source objects after composition.
41+
* (e.g. false)
4042
*/
41-
function compose_file(string $bucketName, string $firstObjectName, string $secondObjectName, string $targetObjectName): void
43+
function compose_file(string $bucketName, string $firstObjectName, string $secondObjectName, string $targetObjectName, bool $deleteSourceObjects = false): void
4244
{
4345
$storage = new StorageClient();
4446
$bucket = $storage->bucket($bucketName);
@@ -60,6 +62,14 @@ function compose_file(string $bucketName, string $firstObjectName, string $secon
6062
$firstObjectName,
6163
$secondObjectName
6264
);
65+
66+
if ($deleteSourceObjects) {
67+
foreach ($objectsToCompose as $sourceObject) {
68+
$bucket->object($sourceObject)->delete();
69+
}
70+
printf(' and source objects were deleted');
71+
}
72+
printf(PHP_EOL);
6373
}
6474
}
6575
# [END storage_compose_file]

‎storage/test/ObjectsTest.php‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ public function provideMoveObject()
260260
return [[true], [false]];
261261
}
262262

263-
public function testCompose()
263+
/**
264+
* @dataProvider provideCompose
265+
*/
266+
public function testCompose(bool $deleteSourceObjects)
264267
{
265268
$bucket = self::$storage->bucket(self::$bucketName);
266269
$object1Name = uniqid('compose-object1-');
@@ -269,28 +272,55 @@ public function testCompose()
269272
$bucket->upload('content', ['name' => $object2Name]);
270273

271274
$targetName = uniqid('compose-object-target-');
272-
$output = self::runFunctionSnippet('compose_file', [
275+
276+
$args = [
273277
self::$bucketName,
274278
$object1Name,
275279
$object2Name,
276280
$targetName,
277-
]);
281+
];
278282

279-
$this->assertEquals(
280-
sprintf(
281-
'New composite object %s was created by combining %s and %s',
282-
$targetName,
283-
$object1Name,
284-
$object2Name
285-
),
286-
$output
283+
if ($deleteSourceObjects) {
284+
$args[] = true;
285+
}
286+
287+
$output = self::runFunctionSnippet('compose_file', $args);
288+
289+
$expectedOutput = sprintf(
290+
'New composite object %s was created by combining %s and %s',
291+
$targetName,
292+
$object1Name,
293+
$object2Name
287294
);
288295

289-
$bucket->object($object1Name)->delete();
290-
$bucket->object($object2Name)->delete();
296+
if ($deleteSourceObjects) {
297+
$expectedOutput .= ' and source objects were deleted';
298+
}
299+
$expectedOutput .= PHP_EOL;
300+
301+
$this->assertEquals($expectedOutput, $output);
302+
303+
if ($deleteSourceObjects) {
304+
$this->assertFalse($bucket->object($object1Name)->exists());
305+
$this->assertFalse($bucket->object($object2Name)->exists());
306+
} else {
307+
$this->assertTrue($bucket->object($object1Name)->exists());
308+
$this->assertTrue($bucket->object($object2Name)->exists());
309+
}
310+
311+
if (!$deleteSourceObjects) {
312+
$bucket->object($object1Name)->delete();
313+
$bucket->object($object2Name)->delete();
314+
}
315+
291316
$bucket->object($targetName)->delete();
292317
}
293318

319+
public function provideCompose()
320+
{
321+
return [[true], [false]];
322+
}
323+
294324
public function testUploadAndDownloadObjectFromMemory()
295325
{
296326
$objectName = 'test-object-' . time();

0 commit comments

Comments
 (0)