当使用 asyncio.run 运行时,azure blob 的代码 aio download_blob 工作一次但不是两次,这看起来像是与 iohttp 相关的错误,但不知道如何解决它。 (Windows)
我的代码几乎是他们原始示例的副本: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/samples/blob_samples_hello_world_async.py
from azure.storage.blob.aio import ContainerClient
from azure.identity import DefaultAzureCredential
credentials = DefaultAzureCredential()
async def test(conn_client):
async with conn_client as client_conn:
stream = await client_conn.download_blob(my_path)
data = await stream.readall()
return data
if __name__ == "__main__":
my_container_name = "Container name"
my_client = ContainerClient.from_container_url(container_url=my_container_name, credential=credentials)
my_path = 'test_path'
data = asyncio.run(test(my_client)) # works and returns the file from blob storage
data2 = asyncio.run(test(my_client)) # doesn't work
错误信息:
DEBUG - asyncio: Using proactor: IocpProactor
...
await self.open()
File "C...\Cache\virtualenvs\transformer-wi-nHELc-py3.11\Lib\site-packages\azure\core\pipeline\transport\_aiohttp.py", line 127, in open
await self.session.__aenter__()
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__aenter__'. Did you mean: '__delattr__'?
Process finished with exit code 1
有什么想法或解决方法吗?
aio download_blob 在使用 asyncio.run 运行时工作一次但不能工作两次 有任何想法或解决方法吗?
该错误表明 Azure SDK 代码正在尝试进入
None
的会话。当 aiohttp
会话在第二次下载操作之前关闭时,可能会发生这种情况。
您可以传递列表中的目标文件路径并使用该列表调用该函数。
这是我将 Blob 从 Blob 存储下载两次到本地环境的代码。
代码:
from azure.storage.blob.aio import ContainerClient
from azure.identity import DefaultAzureCredential
import asyncio
async def download_blob(conn_client,path,l):
for i in l:
with open(i, "wb") as my_blob:
stream = await conn_client.download_blob(path)
data = await stream.readall()
my_blob.write(data)
my_blob.close()
async def main():
my_container_url = "https://xxxxx.blob.core.windows.net/test"
credentials = DefaultAzureCredential()
my_client = ContainerClient.from_container_url(container_url=my_container_url, credential=credentials)
my_path = 'your blob name'
l = [r"C:\Users\xx\xxxx\samp.csv",r"C:\Users\xxxxxx\samp1.csv"]
await download_blob(my_client, my_path,l)
await my_client.close()
if __name__ == "__main__":
asyncio.run(main())
上面的代码执行并将 CSV 文件下载两次到我的本地文件夹。
文件: