【问题标题】:Copy file from Azure Storage blob (Containers) to Azure File shares using Nodejs使用 Nodejs 将文件从 Azure 存储 blob(容器)复制到 Azure 文件共享
【发布时间】:2022-01-07 21:13:57
【问题描述】:

有没有办法将文件从 Azure 容器 (blob) 复制到 Azure 文件共享?

我能够将文件从一个容器复制到另一个容器 - 见下文。
但我想将文件从 Blob 复制到文件共享

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

【问题讨论】:

  • 您可以将文件从 Azure 文件复制到 Azure blob,请参阅此 github repo 中的示例 js 代码,请参阅文件 basic.js 和 advanced.js 了解如何使用文件共享客户端。虽然没有从文件复制到 blob 的直接示例,但您可以根据给定的示例轻松找到要使用的 API/方法。
  • 感谢@AnandSowmithiran。我尝试按照 advanced.js 中的脚本进行操作,但没有运气。在这个阶段,我必须将文件下载到tmp 文件夹中,然后重新上传到fileShare。

标签: azure azure-blob-storage azure-files azure-storage-files azure-node-sdk


【解决方案1】:

我已经在我的环境中测试过。

要将文件从 Azure 文件共享复制到 Azure Blob 存储,可以使用以下代码:

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

要将文件从 Azure Blob Storage 复制到 Azure File Share,我们可以先将 blob 文件下载到本地,然后再将本地文件上传到 Azure File Share。

您可以使用以下代码将 blob 文件下载到本地:

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

您可以使用以下代码将文件从本地上传到 Azure 文件共享:

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

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 2021-02-22
    • 2019-08-12
    • 1970-01-01
    • 2020-02-29
    • 2016-05-29
    • 2021-06-20
    • 2020-08-18
    • 2021-07-29
    相关资源
    最近更新 更多