Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions storage/src/compose_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@
* (e.g. 'my-object-2')
* @param string $targetObjectName The name of the object to be created.
* (e.g. 'composed-my-object-1-my-object-2')
* @param bool $deleteSourceObjects Whether to delete the source objects after composing.
*/
function compose_file(string $bucketName, string $firstObjectName, string $secondObjectName, string $targetObjectName): void
{
function compose_file(
string $bucketName,
string $firstObjectName,
string $secondObjectName,
string $targetObjectName,
bool $deleteSourceObjects = false
): void {
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);

Expand All @@ -48,17 +54,20 @@ function compose_file(string $bucketName, string $firstObjectName, string $secon
$objectsToCompose = [$firstObjectName, $secondObjectName];

$targetObject = $bucket->compose($objectsToCompose, $targetObjectName, [
'deleteSourceObjects' => $deleteSourceObjects,
'destination' => [
'contentType' => 'application/octet-stream'
]
]);

if ($targetObject->exists()) {
$deletionMessage = $deleteSourceObjects ? ' and the source objects were deleted' : '';
printf(
'New composite object %s was created by combining %s and %s',
'New composite object %s was created by combining %s and %s%s',
$targetObject->name(),
$firstObjectName,
$secondObjectName
$secondObjectName,
$deletionMessage
);
}
Comment on lines 56 to 72

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The deleteSourceObjects option is not supported by the Google\Cloud\Storage\Bucket::compose() method or the underlying Cloud Storage Compose API. Passing this option will either be ignored or cause an API error, and the source objects will not be deleted.

To support this option, you should explicitly delete the source objects after a successful compose operation.

    $targetObject = $bucket->compose($objectsToCompose, $targetObjectName, [
        'destination' => [
            'contentType' => 'application/octet-stream'
        ]
    ]);

    if ($targetObject->exists()) {
        if ($deleteSourceObjects) {
            foreach ($objectsToCompose as $sourceObjectName) {
                $bucket->object($sourceObjectName)->delete();
            }
        }
        $deletionMessage = $deleteSourceObjects ? ' and the source objects were deleted' : '';
        printf(
            'New composite object %s was created by combining %s and %s%s',
            $targetObject->name(),
            $firstObjectName,
            $secondObjectName,
            $deletionMessage
        );
    }

}
Expand Down
28 changes: 23 additions & 5 deletions storage/test/ObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ public function provideMoveObject()
return [[true], [false]];
}

public function testCompose()
/**
* @dataProvider provideCompose
*/
public function testCompose($deleteSourceObjects)
{
$bucket = self::$storage->bucket(self::$bucketName);
$object1Name = uniqid('compose-object1-');
Expand All @@ -274,23 +277,38 @@ public function testCompose()
$object1Name,
$object2Name,
$targetName,
$deleteSourceObjects
]);

$deletionMessage = $deleteSourceObjects ? ' and the source objects were deleted' : '';
$this->assertEquals(
sprintf(
'New composite object %s was created by combining %s and %s',
'New composite object %s was created by combining %s and %s%s',
$targetName,
$object1Name,
$object2Name
$object2Name,
$deletionMessage
),
$output
);

$bucket->object($object1Name)->delete();
$bucket->object($object2Name)->delete();
if ($deleteSourceObjects) {
$this->assertFalse($bucket->object($object1Name)->exists());
$this->assertFalse($bucket->object($object2Name)->exists());
} else {
$this->assertTrue($bucket->object($object1Name)->exists());
$this->assertTrue($bucket->object($object2Name)->exists());
$bucket->object($object1Name)->delete();
$bucket->object($object2Name)->delete();
}
$bucket->object($targetName)->delete();
}

public function provideCompose()
{
return [[true], [false]];
}

public function testUploadAndDownloadObjectFromMemory()
{
$objectName = 'test-object-' . time();
Expand Down