Google\Cloud\Storage\Bucket::compose PHP Method

compose() public method

Please note that all objects to be composed must come from the same bucket. Example: $sourceObjects = ['log1.txt', 'log2.txt']; $singleObject = $bucket->compose($sourceObjects, 'combined-logs.txt'); Use an instance of StorageObject. $sourceObjects = [ $bucket->object('log1.txt'), $bucket->object('log2.txt') ]; $singleObject = $bucket->compose($sourceObjects, 'combined-logs.txt');
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/compose Objects compose API documentation
public compose ( array $sourceObjects, string $name, array $options = [] ) : StorageObject
$sourceObjects array The objects to compose.
$name string The name of the composed object.
$options array [optional] { Configuration options. @type string $predefinedAcl Predefined ACL to apply to the composed object. Acceptable values include, `"authenticatedRead"`, `"bucketOwnerFullControl"`, `"bucketOwnerRead"`, `"private"`, `"projectPrivate"`, and `"publicRead"`. **Defaults to** `"private"`. @type array $metadata Metadata to apply to the composed object. The available options for metadata are outlined at the [JSON API docs](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request-body). @type string $ifGenerationMatch Makes the operation conditional on whether the object's current generation matches the given value. @type string $ifMetagenerationMatch Makes the operation conditional on whether the object's current metageneration matches the given value. }
return StorageObject
    public function compose(array $sourceObjects, $name, array $options = [])
    {
        if (count($sourceObjects) < 2) {
            throw new \InvalidArgumentException('Must provide at least two objects to compose.');
        }
        $options += ['destinationBucket' => $this->name(), 'destinationObject' => $name, 'destinationPredefinedAcl' => isset($options['predefinedAcl']) ? $options['predefinedAcl'] : null, 'destination' => isset($options['metadata']) ? $options['metadata'] : null, 'sourceObjects' => array_map(function ($sourceObject) {
            $name = null;
            $generation = null;
            if ($sourceObject instanceof StorageObject) {
                $name = $sourceObject->name();
                $generation = isset($sourceObject->identity()['generation']) ? $sourceObject->identity()['generation'] : null;
            }
            return array_filter(['name' => $name ?: $sourceObject, 'generation' => $generation]);
        }, $sourceObjects)];
        if (!isset($options['destination']['contentType'])) {
            $options['destination']['contentType'] = Psr7\mimetype_from_filename($name);
        }
        if ($options['destination']['contentType'] === null) {
            throw new \InvalidArgumentException('A content type could not be detected and must be provided manually.');
        }
        unset($options['metadata']);
        unset($options['predefinedAcl']);
        $response = $this->connection->composeObject(array_filter($options));
        return new StorageObject($this->connection, $response['name'], $this->identity['bucket'], $response['generation'], $response);
    }

Usage Example

 /**
  * @dataProvider composeProvider
  */
 public function testComposesObjects($metadata, $objects, $expectedSourceObjects)
 {
     $acl = 'private';
     $destinationBucket = 'bucket';
     $destinationObject = 'combined-files.txt';
     $this->connection->composeObject(['destinationPredefinedAcl' => $acl, 'destination' => $metadata + ['contentType' => 'text/plain'], 'sourceObjects' => $expectedSourceObjects, 'destinationBucket' => $destinationBucket, 'destinationObject' => $destinationObject])->willReturn(['name' => $destinationObject, 'generation' => 1])->shouldBeCalledTimes(1);
     $bucket = new Bucket($this->connection->reveal(), $destinationBucket);
     $object = $bucket->compose($objects, $destinationObject, ['predefinedAcl' => $acl, 'metadata' => $metadata]);
     $this->assertEquals($destinationObject, $object->name());
 }