从 django REST API View 中,我尝试访问存储在 azure 存储 blob 中的文件。我想打开它而不将其下载到文件中,如图here 。读取权限就足够了。
为此,我这样勾勒出我的观点:
import os
from fsspec.implementations.http import HTTPFileSystem
@api_view()
def my_view(request):
url = "https://storageaccount.blob.core.windows.net/container/"
filename = "file.f"
fs = HTTPFileSystem(
container_name=os.environ["AZURE_STORAGE_CONTAINER"],
storage_options={
"account_name": os.environ["AZURE_STORAGE_ACCOUNT"],
"account_key": os.environ["AZURE_STORAGE_KEY"],
},
)
with fs.open(url + filename, "r") as fobj:
ds = somehow.open_dataset(fobj)
return Response({"message": "Data manipulated"}, status=200)
这会产生 FileNotFoundError。
我的问题是:
- 这对于 Azure Blob 存储来说是否可行?如果不是,最接近的是什么?
- 我如何验证 HTTPFileSystem?我觉得我或多或少编了这些关键字,但找不到任何相关信息......
最佳答案
我们还花了一段时间才弄清楚如何从 fsspec 访问 Azure Blob 存储,因此在此处记录它。
在 Azure 门户中,在存储帐户级别(而不是容器级别),我们单击“网络+安全”部分中的“访问 key ”,并创建了 account_key
和 connection_string
。
我们使用这些 key 对值创建了一个 $HOME/.env
文件:
account_key=xxxxxx
connection_string=xxxxxxx
然后在 Python 中,我们做了:
import os
from dotenv import load_dotenv
import fsspec
load_dotenv()
storage_options = {'connection_string':os.environ['connection_string'],
'account_key':os.environ['account_key']}
fs = fsspec.filesystem('abfs',**storage_options)
url = 'abfs://my-blob/my_object'
fs.info(url)
关于django - 如何对 Azure Blob 存储的 fsspec 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62452944/