0

是否有将 Azure 文件共享复制到 Blob 的示例(在 node.js 中)?服务器到服务器,无需事先下载文件。

提前致谢。

我的代码:

    var arrFolders = [];
    arrFolders.push("");
    do 
    {
        let directoryName = arrFolders.pop();
        console.log(`List directories and files under directory ${directoryName}`);
        let i = 1;
        const directoryClient = shareClient.getDirectoryClient(directoryName);
        for await (const entity of directoryClient.listFilesAndDirectories()) 
        {
            if (entity.kind === "directory") 
            {
                console.log(`${i++} - directory\t: ${entity.name}`);
                arrFolders.push((directoryName=="") ? entity.name : directoryName + "\\" + entity.name);
            } 
            else 
            {
                console.log(`${i++} - file\t: ${entity.name}`);
                // Copy Files to Blob....
                // ?????
                
            }
        }
        
    } while (arrFolders.length > 0);
    console.log("End list directories and files");
4

1 回答 1

1

如果要将 Azure 文件复制到 Azure Blob,我们可以使用其余 API Copy Blob在此处输入图像描述

在node sdk中,我们可以使用BlobClient.beginCopyFromURL包中的方法@azure/storage-blob来实现它。

例如

const { BlobServiceClient } = require("@azure/storage-blob");
const {
  ShareClient,
  generateAccountSASQueryParameters,
  StorageSharedKeyCredential,
  AccountSASResourceTypes,
  AccountSASPermissions,
  AccountSASServices,
} = require("@azure/storage-file-share");

const fileAccountName = "";
const fileAccountKey ="";

const blobConStr ="";

async function copy() {
  // create account sas token for file service
  var fileCreds = new StorageSharedKeyCredential(
    fileAccountName,
    fileAccountKey
  );
  var accountSas = generateAccountSASQueryParameters(
    {
      startsOn: new Date(new Date().valueOf() - 8640),
      expiresOn: new Date(new Date().valueOf() + 86400000),
      resourceTypes: AccountSASResourceTypes.parse("sco").toString(),
      permissions: AccountSASPermissions.parse("rwdlc").toString(),
      services: AccountSASServices.parse("f").toString(),
    },
    fileCreds
  ).toString();

  //get file share client
  var shareClient = new ShareClient(
    `https://${fileAccountName}.file.core.windows.net/<shareName>`,
    fileCreds
  );
  //get blob container client
  var blobServiceClient = BlobServiceClient.fromConnectionString(blobConStr);
  var containerClient = blobServiceClient.getContainerClient("<containerName>");
  await containerClient.createIfNotExists();

  // list files and copy files to azure blob
  var arrFolders = [];
  arrFolders.push("input");
  do {
    let directoryName = arrFolders.pop();
    console.log(`List directories and files under directory ${directoryName}`);
    let i = 1;
    const directoryClient = shareClient.getDirectoryClient(directoryName);
    for await (const entity of directoryClient.listFilesAndDirectories()) {
      if (entity.kind === "directory") {
        console.log(`${i++} - directory\t: ${entity.name}`);
        arrFolders.push(
          directoryName == "" ? entity.name : directoryName + "\\" + entity.name
        );
      } else {
        console.log(`${i++} - file\t: ${entity.name}`);
        var fileClient = directoryClient.getFileClient(entity.name);
        var soureUrl = fileClient.url + "?" + accountSas;
        try {
          var res = await (
            await containerClient
              .getBlobClient(entity.name)
              .beginCopyFromURL(soureUrl)
          ).pollUntilDone();
          console.log(res.copyStatus);
        } catch (error) {
          throw error;
        }
      }
    }
  } while (arrFolders.length > 0);
}

在此处输入图像描述

于 2021-03-24T07:25:38.507 回答