Azure-sdk-for-js: [Storage] How to copy/rename a blob?

Created on 1 Feb 2020  路  6Comments  路  Source: Azure/azure-sdk-for-js

Forked from https://github.com/Azure/azure-sdk-for-js/issues/7194#issuecomment-580905453

@jtsmedley

jtsmedley commented 2 hours ago
Just start over on the documentation. It has been a nightmare to even try to find how to copy/rename a blob.

Really makes me want to switch to lesser known libraries with documentation that is readable. Your API reference is not.

@maggiepint

Member
maggiepint commented 2 hours ago
Hi @jtsmedley - this is something that I'm actively working on right now. We know that legibility in the refdocs can be problematic sometimes. I would love to know more details of what your biggest pain points are in more detail. Happy to follow up in this thread or to have a call if you were interested.

@jtsmedley

jtsmedley commented 2 hours ago
@maggiepint My main issue today is there is no reference to how to rename a blob which appears to need a copy instead, but I can't get the copy to work off the documentation.

@AlexGhiondea

Member
Author
AlexGhiondea commented 1 hour ago
@jtsmedley can you share more about the package you are using?

Client Docs Storage bug customer-reported

All 6 comments

@jtsmedley

The Azure Storage Blob service doesn't provide a REST api to rename blobs. We didn't provide a helper function in our library either. This sounds like a useful method that we may want to provide either in @azure/storage-blob, or another utility/convenience library on top of @azure/storage-blob. /cc @bterlson @XiaoningLiu @jiacfan @ljian3377

Meanwhile here's one approach that we could use as a workaround:

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;
}

Copy to rename seems quite inefficient to me. Can we do better?

Copying should be completed immediately within the same storage account. I am not aware of other ways unless service add support for it?

We won't support it as a helper function as it won't be guaranteed to be an atomic operation if we are calling copy blob underneath.
Should probably add more documentation on how to copy and rename a blob.

@ljian3377,

Should probably add more documentation on how to copy and rename a blob.

Where are you thinking of adding this documentation?

@ljian3377,

Should probably add more documentation on how to copy and rename a blob.

Where are you thinking of adding this documentation?

@ramya-rao-a Probably we can add a sample for copy since the asynchronous copy scenario is complicated. And mention rename in that sample description in samples/README.md?

@jtsmedley
We do have @azure/storage-file-datalake which support renaming blob (HNS enabled) naturally.

Was this page helpful?
0 / 5 - 0 ratings