使用 node.js 将 Azure 文件共享复制到 Blob
Copy Azure File Share to Blob with node.js
是否有将 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");
如果要将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);
}
是否有将 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");
如果要将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);
}