Azure Storage 是微软 Azure 云平台提供的一种存储服务,可以存储各种类型的数据,如文本、二进制数据和大型对象等。使用 Azure Storage 可以轻松地在应用程序中存储和检索数据,并将其用于用户分析、网站内容、备份和归档等用途。
npm 包 azure-storage 提供了一个 Node.js 客户端库,可用于访问 Azure Storage。在本教程中,我们将介绍如何使用 azure-storage 包来连接到 Azure Storage,执行基本任务,如创建容器,上传文件和下载文件。我们还将包括有关如何处理错误和调试代码的提示。
安装 azure-storage 包
首先,在您的项目目录下运行以下命令来安装 azure-storage 包:
npm install azure-storage
连接到 Azure Storage
要连接到 Azure Storage,您需要提供您的 Azure 存储帐户名称和密钥。它们可以在 Azure 门户中找到。使用以下代码连接到 Azure Storage:
const { BlobServiceClient } = require("@azure/storage-blob"); const connectionString = "DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<your-account-key>;EndpointSuffix=core.windows.net"; const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
请将 <your-account-name>
替换为您的 Azure 存储帐户名称,将 <your-account-key>
替换为您的 Azure 存储帐户密钥。这将创建一个 blobServiceClient 对象,您可以使用它来执行操作。
创建容器
在 Azure 存储中,文件是存储在容器 (container) 中的。要创建一个容器,请使用以下代码:
-- -------------------- ---- ------- ----- ------------- - ------------------------ ----- -------- ----------------- - ----- --------------- - ---------------------------------------------------- ----- ----------------------- - ----- ------------------------- ---------------------- ------------------ --- ------- --------------- ----------------------------------- - ----------------------------- -- - --------------------------- ---展开代码
请将 <your-container-name>
替换为您想要创建的容器的名称。此代码将创建一个名为 <your-container-name>
的容器。
上传文件
要上传文件,请使用下面的代码:
-- -------------------- ---- ------- ----- - --- ------ - - ---------------- ----- -- - -------------- ----- ------- - ------ -------- ----- -------- - ------------------ ----- -------- ------------ - ----- --------------- - ---------------------------------------------------- ----- --------------- - --------------------------------------------- ----- ------------------ - ----- ------------------------------- ---------------- ----------------- ------------- --- -------- --------------- ------------------------------ - ------------------------ -- - --------------------------- ---展开代码
在这个例子里,我们创建了一个文本文件并将其上传到名为 <your-container-name>
的容器。请注意,我们使用了 npm 包 uuid
来生成唯一的文件名。你需要先安装 uuid
包,使用以下命令:
npm install uuid
下载文件
要下载文件,请使用以下代码:
-- -------------------- ---- ------- ----- -------- -------------- - ----- --------------- - ---------------------------------------------------- ----- ---------- - ---------------------------------------- ----- ------------------------- - ----- ---------------------- ----- ----------------- - ------ ------------------------------------------------------------------------- ----------------- ------------- --- ---------- ------------- ---------- ------------------- - ----- -------- ------------------------------ - ------ --- ----------------- ------- -- - ----- ------ - --- ------------------------- ------ -- - ---------------- ---------- ------ - ---- - ------------------- --- ------------------------ -- -- - ------------------------------- --- -------------------------- -------- --- - -------------------------- -- - --------------------------- ---展开代码
请注意,我们需要将
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/53742