npm包 @azure/storage-blob的使用教程

阅读时长 7 分钟读完

在前端开发中,Azure存储库是一种常见的存储服务,可以用于存储和管理数据。Azure Storage Blob就是一种Blob(二进制大型对象)存储服务,可以通过npm包@azure/storage-blob轻松地使用它。该包提供了一组API,可以将文件、图片等二进制数据存储到Azure Blob存储中,并从中读取,同时支持Blob的许多其他功能,例如上传、下载、复制、删除Blob对象。

安装 @azure/storage-blob

在使用@azure/storage-blob之前,您需要先安装它。您可以使用npm来安装该包,只需要在命令行输入以下命令:

npm install @azure/storage-blob

上传文件

使用@azure/storage-blob时,您需要先创建一个blobServiceClient对象,然后使用此对象创建一个containerClient对象来上传/下载/删除blob对象。下面的代码演示了如何使用@azure/storage-blob上传文件:

-- -------------------- ---- -------
----- - ----------------- - - -------------------------------

----- -------- ------ -
    ----- ---------------- - ---------------------------------------------------------------------------------------------------------------------------
    ----- ------------- - --------------
    ----- -------- - ---------

    ----- ----------------- - ---------------------------------------------------------
    ----- --------------- - ----------------------------------------------------
    ----- --------------- - ---------------------------------------------

    ----- ---- - ------- ----- ------- -------

    ----- ---------------------------- -------------
    ----------------- ------------- -- ------------
-

------------------ -- -
    -------------------
---
展开代码

在上面的代码中,我们首先获取了一个blobServiceClient对象。在创建blobServiceClient对象时,我们需要提供Azure Blob存储的连接字符串,包括帐户名称、访问密钥和终结点。然后,我们通过调用blobServiceClient.getContainerClient(containerName)获取一个containerClient对象,这个对象代表了要上传的容器。接下来,我们使用containerClient来获取blockBlobClient对象,并将文件上传到Azure Blob存储的指定容器。

下载文件

使用@azure/storage-blob时,还可以轻松地获取要下载的blob对象。下面的代码演示了如何使用@azure/storage-blob下载文件:

-- -------------------- ---- -------
----- - ----------------- - - -------------------------------

----- -------- ------ -
    ----- ---------------- - ---------------------------------------------------------------------------------------------------------------------------
    ----- ------------- - --------------
    ----- -------- - ---------

    ----- ----------------- - ---------------------------------------------------------
    ----- --------------- - ----------------------------------------------------
    ----- --------------- - ---------------------------------------------

    ----- ------------------------- - ----- ---------------------------

    ----------------------- ---- -------- ------- -----------------------------------------------------------------
-

----- -------- ------------------------------ -
    ------ --- ----------------- ------- -- -
        ----- ------ - ---
        ------------------------- ------ -- -
            -----------------------------
        ---
        ------------------------ -- -- -
            -------------------------
        ---
        -------------------------- --------
    --
-

------------------ -- -
    -------------------
---
展开代码

在上面的代码中,我们通过调用blockBlobClient.download()获取要下载的blob对象。然后,我们通过streamToString()函数将blob对象的内容转换为字符串,并在控制台输出。

删除文件

有时在使用Azure Blob存储时,需要删除某些blob对象。下面的代码演示了如何使用@azure/storage-blob删除文件:

-- -------------------- ---- -------
----- - ----------------- - - -------------------------------

----- -------- ------ -
    ----- ---------------- - ---------------------------------------------------------------------------------------------------------------------------
    ----- ------------- - --------------
    ----- -------- - ---------

    ----- ----------------- - ---------------------------------------------------------
    ----- --------------- - ----------------------------------------------------
    ----- --------------- - ---------------------------------------------

    ----- -------------------------
    ----------------- ------------- -- -----------
-

------------------ -- -
    -------------------
---
展开代码

在上面的代码中,我们使用delete()函数删除Azure Blob存储中的blob对象。

总结

在本文中,我们介绍了如何使用@azure/storage-blob包上传/下载/删除blob对象。这个npm包的使用教程非常简单,只需几个简单的步骤即可开始对Azure Blob存储进行操作。此外,@azure/storage-blob还提供了其他功能,例如复制blob对象。希望这篇文章能帮助你了解如何使用Azure Blob存储服务。

来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/azure-storage-blob