Search code examples
javascriptnode.jsazureazure-blob-storageazure-sdk

How to move blob into subdirectory in Azure using node.js


I've got a node.js service that should donwload file from azure, process it and when its done- move it to a "done" subfolder within the same container.

I've been digging in the documentation for over an hour and couldn't find any method to copy a blob or rename it, because if I create it from the beginning as done/blob-name it will visually present it inside subfolder so renaming might also do the trick (I know thats how it works and thats fine with me, I filter the blobs by ignoring all those who contains done/ in their name).

The only thing close enough is the startCopyFromURL which is a member of the "blob" class, but I can't get how to use it- because I only provide a source but no destination... all I ask is for a simple function copyBlob(sourceBlobURL, dstBlobUrl) -.-

strangely enough the Azure portal does not give any copy / renaming options, but there is something called "storage explorer" (which is accessible through azure portal) and as far as I understood it- its a GUI enabling advanced operations on blobs, including copy operations and sub-folders management. So if there's a GUI to do that, there must be a programmatic way...

im using the (kinda) new v10 sdk (here), only requiring the @azure/storage-blob part in my script.

Thanks a bunch for your help!

UPDATE: I did find references to a method called "startCopyAsync", but all examples I could find online are from sdks to different languages (c# for example) and are 1-2 years old, while the new JS sdk is officialy the primary sdk for js only since few weeks ago (july '19). Couldn't find any mention of this function in the new JS sdk.


Solution

  • You can use startCopyFromURL, as you yourself assumed. The source is the first argument, and the target is the Blob object you use to call this method. So, the flow is something like:

    1. Create a target Blob object (empty, including all sub-folders you need, like done)
    2. Call startCopyFromURL on it providing the URL of the source
    3. Wait for promise resolution.

    A non-javascript example can be found here.