使用 Nodejs 将文件从 Azure 存储 blob(容器)复制到 Azure 文件共享
Posted
技术标签:
【中文标题】使用 Nodejs 将文件从 Azure 存储 blob(容器)复制到 Azure 文件共享【英文标题】:Copy file from Azure Storage blob (Containers) to Azure File shares using Nodejs 【发布时间】:2022-01-07 21:13:57 【问题描述】:有没有办法将文件从 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()
【问题讨论】:
您可以将文件从 Azure 文件复制到 Azure blob,请参阅此 github repo 中的示例 js 代码,请参阅文件 basic.js 和 advanced.js 了解如何使用文件共享客户端。虽然没有从文件复制到 blob 的直接示例,但您可以根据给定的示例轻松找到要使用的 API/方法。 感谢@AnandSowmithiran。我尝试按照 advanced.js 中的脚本进行操作,但没有运气。在这个阶段,我必须将文件下载到tmp
文件夹中,然后重新上传到fileShare。
【参考方案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 Storage 复制到 Azure File Share,我们可以先将 blob 文件下载到本地,然后再将本地文件上传到 Azure File Share。
您可以使用以下代码将 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()
【讨论】:
以上是关于使用 Nodejs 将文件从 Azure 存储 blob(容器)复制到 Azure 文件共享的主要内容,如果未能解决你的问题,请参考以下文章
从 react js / node js 将文件对象上传到天蓝色存储
JavaScript Azure Blob 存储移动 blob
我想通过 NodeJS Express 应用程序将 Azure blob 存储与 Angular 应用程序连接起来
在nodejs中使用azure blob存储触发器在另一个容器中压缩和写入文件