D'Arcy Rail-Ip
D'Arcy Rail-Ip

Reputation: 11965

How can I rename a blob using the PHP Azure-Storage-PHP SDK?

I think I'm losing my mind. I can't seem to find out how to rename a blob using the azure-storage-php SDK located here: https://github.com/Azure/azure-storage-php

This is my current code:

$blobListOptions = new ListBlobsOptions();
$blobListOptions->setPrefix($path);

// List blobs by key.
$blob_list = $blobClient->listBlobs($container, $blobListOptions);
$blobs = $blob_list->getBlobs();

if (count($blobs) > 0) {
    // Only expecting one blob in this path, but looping through regardless.
    foreach($blobs as $blob) {
        $blob->setName($path . 'NEWNAME');
    }
}

Looking at the source code, I can tell that using setName doesn't really do anything at all.

There has to be some method of doing this with the SDK without relying on the REST API.

Upvotes: 1

Views: 671

Answers (1)

Aaron Chen
Aaron Chen

Reputation: 9950

Currently, there is no rename function we can directly use. But we can rename the blob by copying to the new name, then delete the source item.

foreach($blobs as $blob) {
    $blobClient->copyBlob($container, $path . 'NEWNAME', $container, $blob->getName());
    $blobClient->deleteBlob($container, $blob->getName());
}

Upvotes: 2

Related Questions