【问题标题】:Connect azure runbook with file share in azure with python使用 python 将 azure Runbook 与 azure 中的文件共享连接起来
【发布时间】:2021-12-01 20:08:48
【问题描述】:

我有 azure 自动化 Runbook 脚本,我想连接到 azure 中的文件共享。我无法通过运行手册访问文件共享,我尝试了下面的代码,但它不起作用。

conn_str = ''
blob_service_client = BlobServiceClient.from_connection_string(conn_str)

blob_list = blob_service_client.get_container_client('').list_blobs()

for blob in blob_list:
    print(blob.name)

如何使用 python 中的运行手册访问文件共享中的文件?

【问题讨论】:

    标签: python-3.x azure-devops azure-files azure-runbook


    【解决方案1】:

    借助此链接,我设法实现了我想要的:https://docs.microsoft.com/en-us/azure/storage/files/storage-python-how-to-use-file-storage?tabs=python#enumerate-files-and-directories-in-an-azure-file-share

    我使用了这个示例代码:

    file_service = FileService(account_name='', account_key='')
    generator = file_service.list_directories_and_files('')
    for file_or_dir in generator:
        print(file_or_dir.name)
    

    【讨论】:

      【解决方案2】:

      如果您想在 Azure Runbook 中使用 python3 管理 Azure blob,我们需要导入包 azure.storage.blob 及其依赖项。

      第 1 步: a) 下载此软件包:

      pip3 download -d <output dir name> azure-storage-blob==12.8.0
      

      b) 导入这些包

      第 2 步:

      Runbook - 连接代码

      from azure.storage.blob import BlobServiceClient
      
      connect_str = ''
      blob_service_client = BlobServiceClient.from_connection_string(connect_str)
      container_client=blob_service_client.get_container_client('test')
      blobs = container_client.list_blobs( )
      for blob in blobs:
          print(blob.name)
      

      测试和输出:

      【讨论】:

      • 当你说: .get_container_client('test') 是 'test' 容器的名称对吗?
      • Hi @Sofia, container_client=blob_service_client.get_container_client('test') Here get_container_client(container) - 获取客户端以与指定容器交互。这里 (container) 是一个参数,可以是 容器的名称,也可以是 instance ContainerProperties。欲了解更多信息,请参阅here
      • 但是我们可以考虑将 azure 文件共享一个容器吗?
      • @Sofia,不!容器就像文件夹/目录,文件共享就像本地磁盘或公共共享路径
      • 那么,在这种情况下,由于我正在使用文件共享,所以这段代码不能正常工作?
      猜你喜欢
      • 2020-05-07
      • 2021-10-20
      • 1970-01-01
      • 2018-06-22
      • 2019-09-11
      • 2020-04-24
      • 2019-05-18
      • 2012-04-05
      • 1970-01-01
      相关资源
      最近更新 更多