简体   繁体   中英

Copy file from Azure Storage blob (Containers) to Azure File shares using Nodejs

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM