【问题标题】:Access data within the blob storage without downloading无需下载即可访问 Blob 存储中的数据
【发布时间】:2022-01-28 20:59:31
【问题描述】:

我们的客户正在使用 Azure 的 blob 存储服务来保存大文件,以便我们可以使用 Azure 在线服务与他们合作。

我们希望通过 Azure 直接获得的计算资源来读取和使用这些文件,而无需将它们下载到另一个 Azure 服务中,例如 Azure 机器学习工作室。

到目前为止,我们无法访问 blob 存储中的数据,除非将它们下载到 Azure 机器学习工作室中进行处理。

此外,我们要读取的文件都不是以下类型:

但是,它们可以在 python 扩展的帮助下阅读。

我们如何在不事先下载的情况下访问 blob 存储中的数据?
Azure 是否可以将 blob 存储装载到机器学习工作室?
供您参考:我们不需要使用 Azure 机器学习工作室,而是使用具有计算资源的 Azure 在线服务。

此问题与以下 StackOverflow 问题有关:
Azure Blob - Read using Python
read file from azure blob storage in python

【问题讨论】:

标签: python azure azure-blob-storage mount


【解决方案1】:

这是对我们有用的解决方法之一

import os, uuid, sys
from azure.storage.blob import BlockBlobService, PublicAccess
import pandas as pd

blobfile = "<Your BloB Name>"
container = "<Your Container Name>"
blob_account_name = "<Your storage Account>"
sas_token="<Your SAS Token>"

block_blob_service = BlockBlobService(account_name=blob_account_name, sas_token=sas_token)

print("\n File Content")
blob1 = block_blob_service.get_blob_to_text(container, blobfile)
print(blob1.content);

这是供您参考的屏幕截图:-

注意 :- 确保您安装 azure-storage-blob 与 2.1.0 版本,即 pip install azure-storage-blob==2.1.0

这也可以通过 Azure Databricks 实现。试试看Accessing Azure Blob Storage from Azure Databricks

【讨论】:

    【解决方案2】:

    这是适合我的解决方案:

    首先将 Blob-Storage-Container 注册为 Azure 机器学习工作室上的数据存储。
    然后在 Azure Notebook 中:

    from adlfs import AzureBlobFileSystem #pip install adlfs
    from azureml.core import Workspace, Datastore, Dataset
    from azureml.data.datapath import DataPath
    
    # Load the workspace from the saved config file
    ws = Workspace.from_config()
    
    ds = ws.get_default_datastore()
    container_name = ds.container_name
    storage_options = {"account_name": "Storage account name", "account_key": ds.account_key}
    
    fs = AzureBlobFileSystem(**storage_options)
    

    然后您可以使用fs.ls(f"blob-storage-container-name") 和fs.glob(f"blob-storage-container-name/**/*.png") 搜索Blob-Storage-Container。

    fs.isdir('blob-storage-container-name/path/to/folder') 和 fs.isfile('blob-storage-container-name/path/to/file') 也按预期工作。

    您还可以使用 os 获取有关文件位置及其名称的信息。

    import os
    my_path = 'blob-storage-container-name/path/to/file'
    print(os.path.split(my_path))
    

    请注意,您不能像通常使用fs.mkdir()-command 那样创建文件夹!
    相反,当您创建文件时,您可以在 blob-storage-container 中指定应保存文件的位置。

    with fs.open('blob-storage-container-name/path/to/file/Folder1/Folder2/readme.txt', 'w') as f:
        f.write('working')
    

    执行命令后,会看到Folder1和Folder2已经创建完毕。

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2021-10-15
      • 2017-09-16
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多