Azure Blob - 使用 Python 读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48881228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:53:05  来源:igfitidea点击:

Azure Blob - Read using Python

pythonazureazure-storage-blobs

提问by AngiSen

Can someone tell me if it is possible to read a csv file directly from Azure blob storage as a stream and process it using Python? I know it can be done using C#.Net (shown below) but wanted to know the equivalent library in Python to do this.

有人能告诉我是否可以直接从 Azure blob 存储读取 csv 文件作为流并使用 Python 处理它吗?我知道它可以使用 C#.Net(如下所示)来完成,但想知道 Python 中的等效库来做到这一点。

CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("outfiles");
CloudBlob blob = container.GetBlobReference("Test.csv");*

回答by Gaurav Mantri-AIS

Yes, it is certainly possible to do so. Check out Azure Storage SDK for Python

是的,当然可以这样做。查看Azure Storage SDK for Python

from azure.storage.blob import BlockBlobService

block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')

block_blob_service.get_blob_to_path('mycontainer', 'myblockblob', 'out-sunset.png')

You can read the complete SDK documentation here: http://azure-storage.readthedocs.io.

您可以在此处阅读完整的 SDK 文档:http: //azure-storage.readthedocs.io

回答by Sebastian Dziadzio

Here's a way to do it with the new version of the SDK(12.0.0):

以下是使用新版 SDK(12.0.0)执行此操作的方法:

from azure.storage.blob import BlobClient

blob = BlobClient(account_url="https://<account_name>.blob.core.windows.net"
                  container_name="<container_name>",
                  blob_name="<blob_name>",
                  credential="<account_key>")

with open("example.csv", "wb") as f:
    data = blob.download_blob()
    data.readinto(f)

See herefor details.

有关详细信息,请参见此处

回答by Sunil Bisht

Provide Your Azure subscription Azure storage name and Secret Key as Account Key here

在此处提供您的 Azure 订阅 Azure 存储名称和密钥作为帐户密钥

block_blob_service = BlockBlobService(account_name='$$$$$$', account_key='$$$$$$')

This still get the blob and save in current location as 'output.jpg'

这仍然得到 blob 并在当前位置保存为“output.jpg”

block_blob_service.get_blob_to_path('you-container_name', 'your-blob', 'output.jpg')

This will get text/item from blob

这将从 blob 获取文本/项目

blob_item= block_blob_service.get_blob_to_bytes('your-container-name','blob-name')

    blob_item.content

回答by Daniel R

One can stream from blob with python like this:

可以像这样使用 python 从 blob 中流式传输:

from tempfile import NamedTemporaryFile
from azure.storage.blob.blockblobservice import BlockBlobService

entry_path = conf['entry_path']
container_name = conf['container_name']
blob_service = BlockBlobService(
            account_name=conf['account_name'],
            account_key=conf['account_key'])

def get_file(filename):
    local_file = NamedTemporaryFile()
    blob_service.get_blob_to_stream(container_name, filename, stream=local_file, 
    max_connections=2)

    local_file.seek(0)
    return local_file