使用 node.js 将 Azure 文件共享复制到 Blob
Posted
技术标签:
【中文标题】使用 node.js 将 Azure 文件共享复制到 Blob【英文标题】:Copy Azure File Share to Blob with node.js 【发布时间】:2021-06-20 20:05:04 【问题描述】:是否有将 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");
【问题讨论】:
请编辑您的问题并包含您编写的任何代码。同时告诉我们您遇到的任何问题。 【参考方案1】:如果您想将 Azure 文件复制到 Azure Blob,我们可以使用其余 API Copy Blob。
在node sdk中,我们可以使用@azure/storage-blob
包中的BlobClient.beginCopyFromURL
方法来实现。
例如
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);
【讨论】:
以上是关于使用 node.js 将 Azure 文件共享复制到 Blob的主要内容,如果未能解决你的问题,请参考以下文章
将文件从 Azure 文件共享复制到 Azure Blob 的 C# 代码
Azure 管道连接到 ssh 服务器,然后将文件复制到其中
使用 Node.js 服务器为 Next.js 授权 Azure 存储服务 REST API - 从 URL 复制 Blob