Google\Cloud\Storage\StorageObject::rewrite PHP Method

rewrite() public method

This method copies data using multiple requests so large objects can be copied with a normal length timeout per request rather than one very long timeout for a single request. Please note that if the destination bucket is the same as the source bucket and a new name is not provided the source object will be replaced with the copy of itself. Example: Provide your destination bucket as a string and retain the source object's name. $rewrittenObject = $object->rewrite('otherBucket'); Provide your destination bucket as a bucket object and choose a new name for the copied object. $otherBucket = $storage->bucket('otherBucket'); $rewrittenObject = $object->rewrite($otherBucket, [ 'name' => 'newFile.txt' ]); Rotate customer-supplied encryption keys. $key = file_get_contents(__DIR__ . '/key.txt'); $destinationKey = base64_encode(openssl_random_pseudo_bytes(32)); // Make sure to remember your key. $rewrittenObject = $object->rewrite('otherBucket', [ 'encryptionKey' => $key, 'destinationEncryptionKey' => $destinationKey ]);
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite Objects rewrite API documentation.
See also: https://cloud.google.com/storage/docs/encryption#customer-supplied Customer-supplied encryption keys.
public rewrite ( Bucket | string $destination, array $options = [] ) : StorageObject
$destination Bucket | string The destination bucket.
$options array [optional] { Configuration options. @type string $name The name of the destination object. **Defaults to** the name of the source object. @type string $predefinedAcl Access controls to apply to the destination object. Acceptable values include `authenticatedRead`, `bucketOwnerFullControl`, `bucketOwnerRead`, `private`, `projectPrivate`, and `publicRead`. @type string $maxBytesRewrittenPerCall The maximum number of bytes that will be rewritten per rewrite request. Most callers shouldn't need to specify this parameter - it is primarily in place to support testing. If specified the value must be an integral multiple of 1 MiB (1048576). Also, this only applies to requests where the source and destination span locations and/or storage classes. @type string $encryptionKey A base64 encoded AES-256 customer-supplied encryption key. It will be neccesary to provide this when a key was used during the object's creation. @type string $encryptionKeySHA256 Base64 encoded SHA256 hash of the customer-supplied encryption key. This value will be calculated from the `encryptionKey` on your behalf if not provided, but for best performance it is recommended to pass in a cached version of the already calculated SHA. @type string $destinationEncryptionKey A base64 encoded AES-256 customer-supplied encryption key that will be used to encrypt the rewritten object. @type string $destinationEncryptionKeySHA256 Base64 encoded SHA256 hash of the customer-supplied destination encryption key. This value will be calculated from the `destinationEncryptionKey` on your behalf if not provided, but for best performance it is recommended to pass in a cached version of the already calculated SHA. @type string $ifGenerationMatch Makes the operation conditional on whether the destination object's current generation matches the given value. @type string $ifGenerationNotMatch Makes the operation conditional on whether the destination object's current generation does not match the given value. @type string $ifMetagenerationMatch Makes the operation conditional on whether the destination object's current metageneration matches the given value. @type string $ifMetagenerationNotMatch Makes the operation conditional on whether the destination object's current metageneration does not match the given value. @type string $ifSourceGenerationMatch Makes the operation conditional on whether the source object's current generation matches the given value. @type string $ifSourceGenerationNotMatch Makes the operation conditional on whether the source object's current generation does not match the given value. @type string $ifSourceMetagenerationMatch Makes the operation conditional on whether the source object's current metageneration matches the given value. @type string $ifSourceMetagenerationNotMatch Makes the operation conditional on whether the source object's current metageneration does not match the given value. }
return StorageObject
    public function rewrite($destination, array $options = [])
    {
        $options['useCopySourceHeaders'] = true;
        $destinationKey = isset($options['destinationEncryptionKey']) ? $options['destinationEncryptionKey'] : null;
        $destinationKeySHA256 = isset($options['destinationEncryptionKeySHA256']) ? $options['destinationEncryptionKeySHA256'] : null;
        $options = $this->formatDestinationRequest($destination, $options);
        do {
            $response = $this->connection->rewriteObject($options);
            $options['rewriteToken'] = isset($response['rewriteToken']) ? $response['rewriteToken'] : null;
        } while ($options['rewriteToken']);
        return new StorageObject($this->connection, $response['resource']['name'], $response['resource']['bucket'], $response['resource']['generation'], $response['resource'], $destinationKey, $destinationKeySHA256);
    }

Usage Example

 /**
  * @expectedException \InvalidArgumentException
  */
 public function testRewriteObjectThrowsExceptionWithInvalidType()
 {
     $object = new StorageObject($this->connection->reveal(), 'object.txt.', 'bucket');
     $copiedObject = $object->rewrite($object);
 }