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

copy() public method

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. $copiedObject = $object->copy('otherBucket'); Provide your destination bucket as a bucket object and choose a new name for the copied object. $otherBucket = $storage->bucket('otherBucket'); $copiedObject = $object->copy($otherBucket, [ 'name' => 'newFile.txt' ]);
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/copy Objects copy API documentation.
public copy ( 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 $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 $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 copy($destination, array $options = [])
    {
        $key = isset($options['encryptionKey']) ? $options['encryptionKey'] : null;
        $keySHA256 = isset($options['encryptionKeySHA256']) ? $options['encryptionKeySHA256'] : null;
        $response = $this->connection->copyObject($this->formatDestinationRequest($destination, $options));
        return new StorageObject($this->connection, $response['name'], $response['bucket'], $response['generation'], $response, $key, $keySHA256);
    }

Usage Example

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