Python库用于打开Azure Blob存储对象的文件句柄,类似于AWS S3的s3fs库

1 投票
1 回答
70 浏览
提问于 2025-04-14 17:32

对于AWS S3,有一个叫做 s3fs 的Python库,它可以用来打开S3对象的文件句柄。

比如说:

import s3fs
s3 = s3fs.S3FileSystem(anon=True)
with s3.open('my-bucket/my-file.txt', 'rb') as f:
    print(f.read())

那Azure Blob Storage有没有类似的东西呢?

1 个回答

1

有没有类似的东西可以用在Azure Blob Storage上?

现在,你可以使用azure-storage-blob这个Python库来打开Azure Blob Storage中的文件。

另外,你也可以用fsspec来从Azure Blob Storage读取文本文件。

代码:

import fsspec

account_name = "venkat789"
account_key = "zzzzz"

with fsspec.open(f'abfss://<container name>/readfile.txt', account_name=account_name, account_key=account_key) as f:
    data = f.read().decode('utf-8')
    print(data)

上面的代码使用了fsspec库来从Azure Blob Storage读取一个文件。它通过account_nameaccount_key这两个变量进行身份验证。fsspec.open()函数用来打开位于abfss://sample/readfile.txt的文件。接着,使用f.read()读取文件内容,并通过decode()将其转换为字符串。最后,使用print()将文件内容打印到控制台。

输出:

Hello there, Azure Storage. I'm a friendly file ready to be stored in a blob.

在这里输入图片描述

撰写回答