azure-sdk-for-js icon indicating copy to clipboard operation
azure-sdk-for-js copied to clipboard

[Storage] How to copy/rename a blob?

Open jeremymeng opened this issue 5 years ago • 8 comments

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?

jeremymeng avatar Jan 31 '20 23:01 jeremymeng

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

jeremymeng avatar Feb 01 '20 00:02 jeremymeng

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

ljian3377 avatar Feb 13 '20 12:02 ljian3377

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

jeremymeng avatar Feb 13 '20 21:02 jeremymeng

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 avatar Oct 21 '20 08:10 ljian3377

@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 avatar Feb 02 '21 06:02 ramya-rao-a

@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.

ljian3377 avatar Feb 02 '21 09:02 ljian3377

@EmmaZhu can you please take this one?

amishra-dev avatar Mar 01 '22 01:03 amishra-dev

I'll add a sample code to using async copy to rename a blob.

EmmaZhu avatar Mar 01 '22 04:03 EmmaZhu