下载 Azure 存储容器中的所有 blob

2023-12-30

我已经成功编写了一个 python 脚本来列出容器内的所有 blob。

import azure
from azure.storage.blob import BlobService
from azure.storage import *

blob_service = BlobService(account_name='<CONTAINER>', account_key='<ACCOUNT_KEY>')


blobs = []
marker = None
while True:
    batch = blob_service.list_blobs('<CONAINER>', marker=marker)
    blobs.extend(batch)
    if not batch.next_marker:
        break
    marker = batch.next_marker
for blob in blobs:
    print(blob.name)

就像我说的,这只列出了我想要下载的 blob。我已转向 Azure CLI,看看这是否可以帮助我完成我想做的事情。我可以使用以下命令下载单个 blob

azure storage blob download [container]

然后它提示我指定一个可以从 python 脚本中获取的 blob。我必须下载所有这些 blob 的方法是将它们复制并粘贴到上面使用的命令之后的提示中。有没有办法我可以:

A。编写一个 bash 脚本,通过执行命令来迭代 Blob 列表,然后在提示符中粘贴下一个 Blob 名称。

B。指定在 python 脚本或 Azure CLI 中下载容器。有什么东西我没有看到下载整个容器吗?


@gary-liu-msft 解决方案是正确的。我对其进行了更多更改,现在代码可以迭代容器及其中的文件夹结构(PS - 容器中没有文件夹,只有路径),检查客户端中是否存在相同的目录结构,如果不存在则创建该目录结构并下载这些路径中的 blob。它支持带有嵌入子目录的长路径。

from azure.storage.blob import BlockBlobService
from azure.storage.blob import PublicAccess
import os

#name of your storage account and the access key from Settings->AccessKeys->key1
block_blob_service = BlockBlobService(account_name='storageaccountname', account_key='accountkey')

#name of the container
generator = block_blob_service.list_blobs('testcontainer')

#code below lists all the blobs in the container and downloads them one after another
for blob in generator:
    print(blob.name)
    print("{}".format(blob.name))
    #check if the path contains a folder structure, create the folder structure
    if "/" in "{}".format(blob.name):
        print("there is a path in this")
        #extract the folder path and check if that folder exists locally, and if not create it
        head, tail = os.path.split("{}".format(blob.name))
        print(head)
        print(tail)
        if (os.path.isdir(os.getcwd()+ "/" + head)):
            #download the files to this directory
            print("directory and sub directories exist")
            block_blob_service.get_blob_to_path('testcontainer',blob.name,os.getcwd()+ "/" + head + "/" + tail)
        else:
            #create the diretcory and download the file to it
            print("directory doesn't exist, creating it now")
            os.makedirs(os.getcwd()+ "/" + head, exist_ok=True)
            print("directory created, download initiated")
            block_blob_service.get_blob_to_path('testcontainer',blob.name,os.getcwd()+ "/" + head + "/" + tail)
    else:
        block_blob_service.get_blob_to_path('testcontainer',blob.name,blob.name)

相同的代码也可以在这里找到https://gist.github.com/brijrajsingh/35cd591c2ca90916b27742d52a3cf6ba https://gist.github.com/brijrajsingh/35cd591c2ca90916b27742d52a3cf6ba

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

下载 Azure 存储容器中的所有 blob 的相关文章

随机推荐

  • 搜索不适用于 JQGrid 中的过滤器工具栏

    我在稍后阶段 而不是在退出网格时 加载 JQGrid 中的数据时遇到问题 我正在使用过滤器工具栏进行搜索 以下是我正在使用的代码 创建网格 jQuery list jqGrid datatype local colNames my col
  • 找到任意大数的算法

    这是我一直在思考的事情 假设你有一个数字 x 它可以无限大 你必须找出它是什么 您只知道另一个数字 y 是否大于或小于 x 找到 x 的最快 最好的方法是什么 一个邪恶的对手以某种方式选择了一个非常大的数字 说 int x 9 9 9 9
  • Java 是否有带有删除策略的有界集合? [复制]

    这个问题在这里已经有答案了 可能的重复 Java 中保存最后 N 个元素的大小受限队列 https stackoverflow com questions 5498865 size limited queue that holds last
  • 斯坦福核心 NLP 如何获得概率和误差幅度

    当使用解析器或核心 NLP 中的任何注释时 有没有办法访问概率或误差幅度 为了将我的问题放在上下文中 我试图了解是否有一种方法可以以编程方式检测歧义情况 例如 在下面的句子中 动词 desire 被检测为名词 我希望能够知道我可以从 Cor
  • 需要 JavaScript 支持的页面上的 cURL 请求

    我需要获取 pinnaclesports com 的 HTML 源代码 问题是它检测cookie和JS是否启用 如果没有 它只是返回一些页面说 该网站需要启用 JavaScript 和 Cookie 请更改您的浏览器设置或升级您的浏览器 使
  • 无法在 Ubuntu 20.04 上安装 bazel - EXPKEYSIG 无效

    我正在按照以下说明进行操作https docs bazel build versions master install ubuntu html install with installer ubuntu https docs bazel b
  • 如何在快速弹出视图控制器后传递数据

    我正在制作一个关于书籍的应用程序 在应用程序中 我想让应用程序通过获取 ISBN 条形码 自动填充图书信息 views https i stack imgur com ohz2S png 有2个班 一个是 UploadMain 另一个是 S
  • 如何在没有 Windows 窗体引用的情况下获取屏幕分辨率?

    我需要获取运行测试的桌面的分辨率 以前我是这样获取分辨率的 Screen screen Screen PrimaryScreen int screenWidth screen Bounds Width int screenHeight sc
  • 按 JSON 数据类型 postgres 排序

    我有一个 Postgres 表 其中包含 JSON 类型的列 其中包含一堆 JSON 对象 我想查询表记录并按 JSON 字段中存储的值对结果进行排序 我正在运行查询 但它们没有正确排序 我没有找到大量关于专门订购 JSON 字段类型的文档
  • 为什么我在终端中安装的软件包无法在 Spyder 上运行?

    我已经使用 Jupyter Notebook 一段时间了 效果很好 我今天尝试下载 Spyder 但我在终端中安装的许多软件包无法在 Spyder 上运行 例如 pandas datareader在 Jupyter 笔记本上工作正常 但在
  • Silverlight 3 WCF 服务“CommunicationException”服务器返回错误:NotFound

    我有一个 Silverlight 3 应用程序 95 的时间都成功从 WCF 服务 在同一个 Web 应用程序中 请求数据并显示它 这种情况很少发生 通常如果我快速多次访问该服务 但有时它会在单个请求上发生 每隔一段时间 如果我在短时间内请
  • 如何使用 jquery UI 对话框作为 javascript 确认?

    我阅读了很多关于此问题的问题 但每个解决方案都使用相同的解决方法 在 jquery 对话框中提交表单 如下所示 dialog dialog buttons Confirm function window location href targ
  • Rails:自动重新加载虚拟应用程序中使用的 gem 文件

    开发 gem 时 我经常使用需要 gem 的虚拟 Rails 应用程序 以便在开发过程中尝试 gem 更改 另外 我使用相同的虚拟应用程序进行集成测试 通常 我有宝石 rails foo gem 以及相关的虚拟应用程序 rails foo
  • Azure Hyperledger Fabric 单成员区块链设置

    我开始使用 Azure 托管多节点 Hyperledger 网络 我之前一直在本地环境中运行 但想使用 Azure 我部署了 Hyperledger Fabric 单成员区块链 模板 该模板创建了 5 个虚拟机 每个虚拟机用于 CA 订购者
  • 本地主机上的双栈 ipv6/ipv4

    我有一个 ipv4 服务器 它只接受本地主机上的连接 使用INADDR LOOPBACK 我想将此服务器转换为双栈 ipv6 ipv4 然而 使用in6addr loopback只接受连接到 1 我发现我可以同时接受 ipv4 和 ipv6
  • R model.matrix 在所有列中使用相同的因子集

    我有一组篮球阵容数据 有五列 每列共享相同的因子 如下所示 head dat V1 V2 V3 V4 V5 1 MILES KEATON KINGSLEY MOSES BELL ANTHLON HANNAHS DUSTY DURHAM JA
  • Android:在 RecyclerView 顶部添加分隔线

    我能够在单元格底部添加分隔线RecyclerView this recyclerView layoutManager layoutManager val dividerItemDecoration DividerItemDecoration
  • 如何在 Node.js 中截取窗口屏幕截图?

    我正在研究寻找一种使用 Node js 截取窗口屏幕截图的方法 并且我正在尝试使用 node ffi 来做到这一点 但我不知道如何 一次我 我被困在这里 var ffi require ffi var user32 new ffi Libr
  • Obj-C 中奇怪的 Switch 错误

    我的代码中有这个 switch 语句 switch buttonIndex case 0 actionSheet dismissWithClickedButtonIndex buttonIndex animated YES break ca
  • 下载 Azure 存储容器中的所有 blob

    我已经成功编写了一个 python 脚本来列出容器内的所有 blob import azure from azure storage blob import BlobService from azure storage import blo