0

有没有办法将文件从 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()
4

1 回答 1

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 存储复制到 Azure 文件共享,我们可以先将 blob 文件下载到本地,然后将本地文件上传到 Azure 文件共享。

您可以使用以下代码将 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-12-03T09:36:05.543 回答