0

Is there a way to copy files from Azure Containers (blobs) to Azure File shares?

I was able to copy files from one container to another - see below.
But I wanted to copy files from Blob to File Shares

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

async function copy() {

    const account = "<account-name>";
    const accountKey = "<account-key>";
    const cert = new StorageSharedKeyCredential(account, accountKey)
    const blobServiceClient = new BlobServiceClient(
        `https://${account}.blob.core.windows.net`,
        cert
    );

    const sourceContainer = blobServiceClient.getContainerClient("documents")
    const desContainer = blobServiceClient.getContainerClient("copy")
    //if the desContainer does not exist, please run the following code
    // await desContainer.create()

    //copy blob
    const sourceBlob = sourceContainer.getBlobClient("file1.png");
    console.log(sourceBlob, sourceBlob.name)
    const desBlob = desContainer.getBlobClient(sourceBlob.name)
    const response = await desBlob.beginCopyFromURL(sourceBlob.url);
    const result = (await response.pollUntilDone())
    console.log(result._response.status)
    console.log(result.copyStatus)
}

copy()
Selyst
  • 111
  • 1
  • 12
  • You can copy file from Azure files to Azure blob, refer the sample js code in this [github repo](https://github.com/Azure/azure-sdk-for-js/tree/%40azure/storage-file-share_12.8.0/sdk/storage/storage-file-share/samples/javascript), see files basic.js, and advanced.js on how to use the fileshare client. Though direct example for copy from files to blob is not there, you can easily find which APIs/method to use based on given sample. – Anand Sowmithiran Dec 01 '21 at 09:13
  • thanks @AnandSowmithiran. I've tried to follow the scripts inside advanced.js but no luck. At this stage, I have to download the file in a `tmp` folder and then re-upload it to fileShare. – Selyst Dec 01 '21 at 13:01
  • For future searchers, see azcopy command line tool: https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-files – cbare Nov 02 '22 at 06:44

1 Answers1

1

I have tested in my environment.

To copy a file from Azure File Share to Azure Blob Storage, you can use the below code:

const {
    BlobServiceClient,
    StorageSharedKeyCredential,
} = require("@azure/storage-blob");
const {
    ShareServiceClient
} = require("@azure/storage-file-share")

async function copy() {

    const account = "<account-name>";
    const accountKey = "<account-key>";
    const cert = new StorageSharedKeyCredential(account, accountKey)
    const accountSas = "<account-sas>"
    const blobServiceClient = new BlobServiceClient(
        `https://${account}.blob.core.windows.net`,
        cert
    );
    const serviceClient = new ShareServiceClient(`https://${account}.file.core.windows.net${accountSas}`,cert)
    const sourceContainer = blobServiceClient.getContainerClient("containerName")
    const shareClient = serviceClient.getShareClient("fileShareName")
    const directoryClient = shareClient.getDirectoryClient("directoryName");
    var fileClient = directoryClient.getFileClient("fileName");
    //if the desContainer does not exist, please run the following code
    // await desContainer.create()
    //copy blob
    const sourceBlob = sourceContainer.getBlobClient("blobFileName");
    const response = await sourceBlob.beginCopyFromURL(fileClient.url);
}
copy()

To copy the files from Azure Blob Storage to Azure File Share, we can download the blob file to local first and then upload the local file to Azure File Share.

You can use below code to download the blob file to local:

const {
    BlobServiceClient,
    StorageSharedKeyCredential,
} = require("@azure/storage-blob");
    const account = "<account-name>";
    const accountKey = "<account-key>";
    const cert = new StorageSharedKeyCredential(account, accountKey)
    const accountSas = "<account-sas>"
    
function download() {
    const account = "<account-name>";
    const accountKey = "<account-key>";
    const cert = new StorageSharedKeyCredential(account, accountKey)
    const accountSas = "<account-sas>"
    const container = "containerName"
    const blobFileName = "blobFileName"
    const blobServiceClient = new BlobServiceClient(
        `https://${account}.blob.core.windows.net`,
        cert
    );
    const sourceContainer = blobServiceClient.getContainerClient(container)
    const sourceBlob = sourceContainer.getBlobClient(blobFileName);
    sourceBlob.downloadToFile(blobFileName);
}
download()

You can use the below code to upload the file from local to Azure File Share:

const {
    ShareServiceClient
} = require("@azure/storage-file-share");
function upload() {
    const account = "<account-name>";
    const accountKey = "<account-key>";
    const cert = new StorageSharedKeyCredential(account, accountKey)
    const accountSas = "<account-sas>"
    const serviceClient = new ShareServiceClient(`https://${account}.file.core.windows.net${accountSas}`,cert)
    const shareClient = serviceClient.getShareClient("fileShareName")
    const directoryClient = shareClient.getDirectoryClient("directoryName");
    var fileClient = directoryClient.getFileClient("FileName");
    fileClient.uploadFile("localFilePath");
}
upload()
RamaraoAdapa
  • 2,837
  • 2
  • 5
  • 11