如果要将 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);
}