用Node.js操作AzureStorage[Blob],进行下载
首先
介绍一种用于在本地环境中批量下载Azure存储Blob的程序(使用TypeScript),Azure存储Blob是Azure云存储的一部分,它包含了blob服务的所有容器中的文件。
环境
-
- Node v14.17.0
-
- Windows10
- Typescript
翻译成中文的一种选择:
代码
为了方便在一个文件中进行介绍,我将所有内容都放在了一个文件中。但我认为最好将文件分开以便更容易管理。
package.json文件
{
"name": "azurestorage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && npm run main",
"main": "node index.js",
"clean": "tsc --build --clean"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@azure/arm-resources": "^4.2.0",
"@azure/identity": "^1.3.0",
"@azure/storage-blob": "^12.6.0",
"@azure/storage-file-share": "^12.6.0"
},
"devDependencies": {
"@types/node": "^15.12.2",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
}
}
主要文件
import fs from 'fs'
import { BlobServiceClient, StorageSharedKeyCredential } from "@azure/storage-blob";
interface AzureStorageBlobConfig {
type: 'SAS' | 'CONSTR' | 'KEY',
connStr?: string,//CONSTR
account?: string,//KEY,SAS
accountKey?: string,//KEY
sasToken?: string,
containerName?: string[],
}
const AuthController = (authobj: AzureStorageBlobConfig) => {
switch (authobj.type) {
case 'SAS':
if (authobj.account && authobj.sasToken) {
return AuthSas(authobj.account, authobj.sasToken)
} else {
console.log(authobj.type + 'の認証情報が足りません')
}
case 'CONSTR':
if (authobj.connStr) {
return AuthConstr(authobj.connStr)
} else {
console.log(authobj.type + 'の認証情報が足りません')
}
break
case 'KEY':
if (authobj.account && authobj.accountKey) {
return AuthKey(authobj.account, authobj.accountKey)
} else {
console.log(authobj.type + 'の認証情報が足りません')
}
break
}
}
const AuthSas = (account: string, sastoken: string): BlobServiceClient => {
return new BlobServiceClient(`https://${account}.blob.core.windows.net/${sastoken}`);
}
const AuthConstr = (connstr: string): BlobServiceClient => {
return BlobServiceClient.fromConnectionString(connstr);
}
const AuthKey = (account: string, key: string) => {
const sharedKeyCredential = new StorageSharedKeyCredential(account, key);
return new BlobServiceClient(`https://${account}.blob.core.windows.net`, sharedKeyCredential);
}
//IIFE 即時関数
(async function main() {
try {
const authObj: AzureStorageBlobConfig = {
type: 'CONSTR',
connStr: "{your connection string}",
containerName: ["{your containerNames}"]
}
let blobServiceClient = AuthController(authObj)
if (blobServiceClient) {
let containerNameList = []
//get containerName
if (authObj.containerName) {
for (const container of authObj.containerName) {
containerNameList.push(container)
}
} else {
let containers = blobServiceClient.listContainers();
for await (const container of containers) {
containerNameList.push(container.name)
}
}
//get BlobName
Promise.all(containerNameList.map(async value => {
if (blobServiceClient) {
const containerClient = blobServiceClient.getContainerClient(value);
for await (const blob of containerClient.listBlobsFlat()) {
console.log(blob.name);
const blobClient = containerClient.getBlobClient(blob.name);
fs.mkdirSync(`./download/${value}/` + (path.dirname(blob.name).replace(/\.$/, '')), { recursive: true })
await blobClient.downloadToFile(`./download/${value}/` + blob.name);
}
}
}
))
}
} catch (e) {
console.log(e)
}
})()
使用指南
-
- npm的安装
-
- 在主文件的立即执行函数中,找到标有authObj的位置,输入认证信息。
-
- 认证类型可以选择连接字符串、连接密钥和SAS共有三种。
-
- 通过输入容器名称来下载该容器下的所有文件。
-
- 确认下载文件的保存文件夹
-
- (默认情况下,在主文件目录下会创建一个download文件夹,并将文件保存在其中)
- 执行下载命令npm start。
结束
我这次写了一篇关于Azure Storage Blob下载的文章,
但是我计划今后也会写关于File Shared下载的文章。