Val*_*ecz 5 filesystems django azure django-rest-framework azure-blob-storage
从 django REST API 视图中,我尝试访问存储在 azure 存储 blob 中的文件。我想打开它而不将其下载到文件中,如此处所示。读取访问权限就足够了。
为此,我这样勾勒出我的观点:
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)
Run Code Online (Sandbox Code Playgroud)
这会给出 FileNotFoundError。
我的问题是:
我们还花了一段时间才弄清楚如何从 fsspec 访问 Azure Blob 存储,因此将其记录在此处。
在 Azure 门户中,在存储帐户级别(而不是容器级别),我们单击“网络+安全”部分中的“访问密钥”,并创建了一个account_key和connection_string。
我们创建了一个$HOME/.env包含这些密钥对值的文件:
account_key=xxxxxx
connection_string=xxxxxxx
Run Code Online (Sandbox Code Playgroud)
然后在Python中,我们做了:
account_key=xxxxxx
connection_string=xxxxxxx
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1072 次 |
| 最近记录: |