Azure Storage: Simply Move File

Mike-E-angelo 591 Reputation points
2022-06-12T18:14:31.277+00:00

This is in reference to this question:
https://stackoverflow.com/questions/40260583/azure-storage-move-blob-to-other-container

In my case I simply want to move the file from one location in my blob container to another (same container). Copying takes over 10 seconds currently for a 16MB video file, which seems beyond slow for simply wanting to update a reference from one location to another.

To reiterate, I do not want to copy a file (slow) but move it (fast) :)

Thank you for any assistance/guidance you can provide.

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,539 questions
{count} votes

Accepted answer
  1. Andriy Bilous 11,821 Reputation points MVP Volunteer Moderator
    2022-06-13T09:43:53.907+00:00

    Azure Blob REST API does not dirrectly support Move/Rename operation. You need to create a new blob container with the new name and copy blobs from old blob container to the new one. Once the blobs are copied, you can delete the old blob container.
    Copy blob in Cloud operation is asynchronous, ensure that the blobs are copied completely before deleting the blob container.
    In your case the StartCopyFromUriAsync operation is the only way to move the file from one location in my blob container to another.

    Example:

    async function renameBlob(containerClient, existingName, newName) {  
      // assume blob exists and names are different  
      const blobClient = containerClient.getBlobClient(existingName);  
      const newBlobClient = containerClient.getBlobClient(newName);  
      
      const poller = await newBlobClient.beginCopyFromURL(blobClient.url);  
      await poller.pollUntilDone();  
      
      /* test code to ensure that blob and its properties/metadata are copied over  
      const prop1 = await blobClient.getProperties();  
      const prop2 = await newBlobClient.getProperties();  
      
      if (prop1.contentLength !== prop2.contentLength) {  
        throw new Error("Expecting same size between copy source and destination");  
      }  
      
      if (prop1.contentEncoding !== prop2.contentEncoding) {  
        throw new Error("Expecting same content encoding between copy source and destination");  
      }  
      
      if (prop1.metadata.keya !== prop2.metadata.keya) {  
        throw new Error("Expecting same metadata between copy source and destination");  
      }  
      */  
      
      await blobClient.delete();  
      
      return newBlobClient;  
    }  
    

    https://learn.microsoft.com/en-us/rest/api/storageservices/blob-service-rest-api

    Official information from Microsoft: Azure Blob Move/Rename operation
    https://github.com/Azure/azure-sdk-for-js/issues/7202

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.