0
votes

I have a NodeJS backend which use the official blob storage library (@azure/storage-blob) from Microsoft to manage my Blob Storage:

https://www.npmjs.com/package/@azure/storage-blob

It is necessary to move a blob from one folder, to another. Unfortunately I can't find any documentation for that.

What I did until now is:

const { BlobServiceClient } = require("@azure/storage-blob");

const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.storageconnection);
const containerClient = blobServiceClient.getContainerClient('import');
const blobClient = containerClient.getBlobClient('toImport/' + req.body.file);
const downloadBlockBlobResponse = await blobClient.download();
... do some stuff with the value of the files

Like you can see in the code, I read a file from folder "toImport". After that I want to move the file to another folder "finished". Is that possible? Maybe I need to create a copy of the file and delete the old one?

1
Maybe I need to create a copy of the file and delete the old one? - That's exactly you will need to do. There's no need for you to download and upload. - Gaurav Mantri
But how can I do that. I can't find the commands for that. Is there any method for getBlobClient()? - Nico Schuck

1 Answers

2
votes

As such move operation is not supported in Azure Blob Storage. What you have to do is copy the blob from source to destination, monitor the copy progress (because copy operation is asynchronous) and delete the blob once the copy is complete.

For copying, the method you would want to use is beginCopyFromURL(string, BlobBeginCopyFromURLOptions).

Please see this code:

const { BlobServiceClient } = require("@azure/storage-blob");

const connectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key;EndpointSuffix=core.windows.net";
const container = "container-name";
const sourceFolder = "source";
const targetFolder = "target";
const blobName = "blob.png";

async function moveBlob() {
  const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
  const containerClient = blobServiceClient.getContainerClient(container);
  const sourceBlobClient = containerClient.getBlobClient(`${sourceFolder}/${blobName}`);
  const targetBlobClient = containerClient.getBlobClient(`${targetFolder}/${blobName}`);
  console.log('Copying source blob to target blob...');
  const copyResult = await targetBlobClient.beginCopyFromURL(sourceBlobClient.url);
  console.log('Blob copy operation started successfully...');
  console.log(copyResult);
  do {
    console.log('Checking copy status...')
    const blobCopiedSuccessfully = await checkIfBlobCopiedSuccessfully(targetBlobClient);
    if (blobCopiedSuccessfully) {
      break;
    }
  } while (true);
  console.log('Now deleting source blob...');
  await sourceBlobClient.delete();
  console.log('Source blob deleted successfully....');
  console.log('Move operation complete.');
}

async function checkIfBlobCopiedSuccessfully(targetBlobClient) {
  const blobPropertiesResult = await targetBlobClient.getProperties();
  const copyStatus = blobPropertiesResult.copyStatus;
  return copyStatus === 'success';
}

moveBlob();