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

rename() public method

Please note that there is no atomic rename provided by the Storage API. This method is for convenience and is a set of sequential calls to copy and delete. Upon success the source object's metadata will be cleared, please use the returned object instead. Example: $object2 = $object->rename('object2.txt'); echo $object2->name();
public rename ( string $name, array $options = [] ) : StorageObject
$name string The new name.
$options array [optional] { Configuration options. @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 The renamed object.
    public function rename($name, array $options = [])
    {
        $copiedObject = $this->copy($this->identity['bucket'], ['name' => $name] + $options);
        $this->delete(array_intersect_key($options, ['httpOptions' => null, 'retries' => null]));
        $this->info = [];
        return $copiedObject;
    }

Usage Example

 public function testRenamesObject()
 {
     $sourceBucket = 'bucket';
     $objectName = 'object.txt';
     $newObjectName = 'new-name.txt';
     $acl = 'private';
     $key = base64_encode('abcd');
     $hash = base64_encode('1234');
     $this->connection->copyObject(['sourceBucket' => $sourceBucket, 'sourceObject' => $objectName, 'destinationBucket' => $sourceBucket, 'destinationObject' => $newObjectName, 'destinationPredefinedAcl' => $acl, 'httpOptions' => ['headers' => ['x-goog-encryption-algorithm' => 'AES256', 'x-goog-encryption-key' => $key, 'x-goog-encryption-key-sha256' => $hash]]])->willReturn(['bucket' => $sourceBucket, 'name' => $newObjectName, 'generation' => 1])->shouldBeCalledTimes(1);
     $this->connection->deleteObject(Argument::any())->willReturn([])->shouldBeCalledTimes(1);
     $object = new StorageObject($this->connection->reveal(), $objectName, $sourceBucket);
     $copiedObject = $object->rename($newObjectName, ['predefinedAcl' => $acl, 'encryptionKey' => $key, 'encryptionKeySHA256' => $hash]);
     $this->assertEquals($sourceBucket, $copiedObject->info()['bucket']);
     $this->assertEquals($newObjectName, $copiedObject->info()['name']);
 }