我正在寻找有关如何使用Blob Storage或App Service静态网页功能(在BUILD中宣布)的任何示例,以托管一个二进制文件上传,该上传将进入存储服务所在的存储帐户。
谢谢。
我正在寻找有关如何使用Blob Storage或App Service静态网页功能(在BUILD中宣布)的任何示例,以托管一个二进制文件上传,该上传将进入存储服务所在的存储帐户。
谢谢。
先决条件
设置
让我们准备一个项目,以便使用JavaScript的Azure Blob存储客户端库v12进行工作。
为该项目创建一个新目录。例如:
创建一个项目文件夹mkdir blob-quickstart-v12
进入已创建的文件夹cd blob-quickstart-v12
运行npm init
创建一个“package.json”文件,并确保它看起来像这样:
{
"name": "blob-quickstart-v12",
"version": "1.0.0",
"description": "Use the @azure/storage-blob SDK version 12 to interact with Azure Blob storage",
"main": "blob-quickstart-v12.js",
"scripts": {
"start": "node blob-quickstart-v12.js"
},
"author": "Your Name",
"license": "MIT",
"dependencies": {
"@azure/storage-blob": "^12.0.0",
"@types/dotenv": "^4.0.3",
"dotenv": "^6.0.0"
}
}
安装包依赖 npm install
创建一个名为 "blob-quickstart-v12.js" 的新文件,并复制/粘贴以下代码:
const { BlobServiceClient } = require('@azure/storage-blob');
const uuidv1 = require('uuid/v1');
async function main() {
console.log('azure blob storage v12 - javaScript quickstart sample');
// Quick start code goes here
}
main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));
从Azure门户复制凭据
您的应用程序必须经过授权才能向Azure存储发出请求。我们应将您的存储账户凭据作为连接字符串添加到应用程序中:
登录Azure门户。
找到您的存储账户。
在“设置”部分中,选择“访问密钥”。在那里,您可以查看每个密钥的帐户访问密钥和完整的连接字符串。
复制key1下的连接字符串值并保存。
连接字符串
main
函数中。文件名为"blob-quickstart-v12.js":const AZURE_STORAGE_CONNECTION_STRING = '<paste-your-connection-string-here>';
创建一个容器
让我们创建一个新的容器。下面的代码将一个UUID值附加到容器名称上,以确保它是唯一的。
重要提示:容器名称必须小写!!!
将此代码添加到main
函数的末尾:
// Create the BlobServiceClient object which will be used to create a container client
const blobServiceClient = await BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
// Create a unique name for the container
const containerName = 'mycontainer' + uuidv1();
console.log('creating container: ', containerName);
// Get a reference to a container
const containerClient = await blobServiceClient.getContainerClient(containerName);
// Create the container
const createContainerResponse = await containerClient.create();
console.log("container was created successfully. requestId: ", createContainerResponse.requestId);
将 Blob 上传到容器
以下代码片段将一个字符串上传到 Blob,从已创建的容器部分开始。
将此代码添加到主函数的末尾:
// Create a unique name for the blob
const blobName = 'myfile' + uuidv1() + '.txt';
// Get a block blob client
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
console.log('uploading to azure storage as blob: ', blobName);
// Upload data to the blob
const data = 'Hello, World!';
const uploadBlobResponse = await blockBlobClient.upload(data, data.length);
console.log("blob was uploaded successfully. requestId: ", uploadBlobResponse.requestId);
下载 Blob
通过调用 download 方法来下载先前创建的 blob。示例代码包括一个名为 streamToString
的辅助函数,用于将 Node.js 可读流读取为字符串。
将此代码添加到主函数的末尾:
// Get blob content from position 0 to the end
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
// In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log('downloaded blob content...');
console.log(await streamToString(downloadBlockBlobResponse.readableStreamBody));
// A helper function used to read a Node.js readable stream into a string
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
运行代码
从控制台提示符中,导航到包含“blob-quickstart-v12.js”文件的根目录,然后执行:
node blob-quickstart-v12.js
应用程序的输出类似于以下示例:
azure blob storage v12 - javaScript quickstart sample
creating container: mycontainer-4a0780c0-fb72-11e9-b7b9-b387d3c488da
uploading to azure storage as blob: myfileb4a3128d0-fb72-11e9-b7b9-b387d3c488da.txt
downloaded blob content: Hello, World!
参考文献:Microsoft