I’m using the @azure/storage-blob package to manipulate files in Azure.
In the same Azure storage account I have two storage containers i.e. source and destination.
What I’m trying to do is copy a file located in the source container to the destination container without having to download the file.
I’m currently downloading to a buffer and uploading to the destination.
Is there any way to do this directly?
const blobServiceClient = BlobServiceClient.fromConnectionString(BLOB_CONNECTION_STRING);
const sourceContainerClient = blobServiceClient.getContainerClient(SOURCE_CONTAINER_NAME);
const sourceBlockBlobClient = sourceContainerClient.getBlockBlobClient(filename);
const destinationContainerClient = blobServiceClient.getContainerClient(DESTINATION_CONTAINER_NAME);
const destinationBlockBlobClient = finalContainerClient.getBlockBlobClient(filename);
const sourceFileBuffer = await sourceBlockBlobClient.downloadToBuffer();
destinationBlockBlobClient.uploadData(sourceFileBuffer);
2
Answers
I`ve solved the problem with the following code:
Using the syncCopyFromUrl function I did just pass an URL and the file was copied correctly and faster. In my scenario, I've needed to generate a sasToken to ensure the file was accessible for copying.
You can use the below code to copy a file from one container to another container using javascript SDK.
Code:
Output:
Portal:
Reference:
Get started with Azure Blob Storage and JavaScript – Azure Storage | Microsoft Learn