skip to Main Content

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


  1. Chosen as BEST ANSWER

    I`ve solved the problem with the following code:

    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);
    
    await destinationBlockBlobClient.syncCopyFromURL(`${sourceBlockBlobClient.url}?${sasToken}`);
    

    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.


  2. Copy File between Azure blobs containers.

    You can use the below code to copy a file from one container to another container using javascript SDK.

    Code:

    const { BlobServiceClient } = require('@azure/storage-blob');
    
    const CONNECTION_STRING = '<Your-connection -string>';
    const SOURCE_CONTAINER_NAME = 'test'; //<your-source-container-name>
    const DESTINATION_CONTAINER_NAME = 'test1'; //<Your-destination-container-name>
    const filename = 'sample012.txt';
    
    async function copyFile() {
      const blobServiceClient = BlobServiceClient.fromConnectionString(CONNECTION_STRING);
    
      const sourceContainerClient = blobServiceClient.getContainerClient(SOURCE_CONTAINER_NAME);
      const sourceBlockBlobClient = sourceContainerClient.getBlockBlobClient(filename);
    
      const destinationContainerClient = blobServiceClient.getContainerClient(DESTINATION_CONTAINER_NAME);
      const destinationBlockBlobClient = destinationContainerClient.getBlockBlobClient(filename);
    
      const sourceBlobUrl = sourceBlockBlobClient.url;
      await destinationBlockBlobClient.startCopyFromURL(sourceBlobUrl);
    
      console.log(`File ${filename} copied from ${SOURCE_CONTAINER_NAME} to ${DESTINATION_CONTAINER_NAME}`);
    }
    
    copyFile().catch((err) => {
      console.error('Error:', err.message);
    })
    

    Output:

    File sample012.txt copied from test to test1
    

    enter image description here

    Portal:
    enter image description here

    Reference:

    Get started with Azure Blob Storage and JavaScript – Azure Storage | Microsoft Learn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search