gpt4 book ai didi

python - 如何在 Spyder/Anaconda 中使用 Python 从 Azure Blob 下载/上传文件

转载 作者:行者123 更新时间:2023-12-02 06:37:25 25 4
gpt4 key购买 nike

我是 Azure 新手。我今天创建了一个 Blob 存储。我正在尝试运行一些在网上找到的 Python 代码,以从此 Blob 容器下载文件。这是我正在测试的代码。

# download_blobs.py
# Python program to bulk download blob files from azure storage
# Uses latest python SDK() for Azure blob storage
# Requires python 3.6 or above
import os
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.storage.blob import ContentSettings, ContainerClient

# IMPORTANT: Replace connection string with your storage account connection string
# Usually starts with DefaultEndpointsProtocol=https;...
MY_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=ryanpythonstorage;AccountKey=my_account_key;EndpointSuffix=core.windows.net"

# Replace with blob container
MY_BLOB_CONTAINER = "ryanpythonstorage" #copied from Access Keys

# Replace with the local folder where you want files to be downloaded
LOCAL_BLOB_PATH = "C:\\Users\\ryans\\Desktop\\blob\\"

class AzureBlobFileDownloader:
def __init__(self):
print("Intializing AzureBlobFileDownloader")

# Initialize the connection to Azure storage account
self.blob_service_client = BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)
self.my_container = self.blob_service_client.get_container_client(MY_BLOB_CONTAINER)


def save_blob(self,file_name,file_content):
# Get full path to the file
download_file_path = os.path.join(LOCAL_BLOB_PATH, file_name)

# for nested blobs, create local path as well!
os.makedirs(os.path.dirname(download_file_path), exist_ok=True)

with open(download_file_path, "wb") as file:
file.write(file_content)

def download_all_blobs_in_container(self):
my_blobs = self.my_container.list_blobs()
for blob in my_blobs:
print(blob.name)
bytes = self.my_container.get_blob_client(blob).download_blob().readall()
self.save_blob(blob.name, bytes)

# Initialize class and upload files
azure_blob_file_downloader = AzureBlobFileDownloader()
azure_blob_file_downloader.download_all_blobs_in_container()

这是我运行代码时看到的结果。

Intializing AzureBlobFileDownloader
Traceback (most recent call last):

File "C:\Users\ryans\AppData\Local\Temp\ipykernel_14020\3010845424.py", line 37, in <module>
azure_blob_file_downloader.download_all_blobs_in_container()

File "C:\Users\ryans\AppData\Local\Temp\ipykernel_14020\3010845424.py", line 30, in download_all_blobs_in_container
for blob in my_blobs:

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\core\paging.py", line 129, in __next__
return next(self._page_iterator)

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\core\paging.py", line 76, in __next__
self._response = self._get_next(self.continuation_token)

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\storage\blob\_list_blobs_helper.py", line 79, in _get_next_cb
process_storage_error(error)

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\storage\blob\_shared\response_handlers.py", line 177, in process_storage_error
exec("raise error from None") # pylint: disable=exec-used # nosec

File "<string>", line 1, in <module>

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\storage\blob\_list_blobs_helper.py", line 72, in _get_next_cb
return self._command(

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\storage\blob\_generated\operations\_container_operations.py", line 1479, in list_blob_flat_segment
map_error(status_code=response.status_code, response=response, error_map=error_map)

File "C:\Users\ryans\anaconda3\lib\site-packages\azure\core\exceptions.py", line 105, in map_error
raise error

ResourceNotFoundError: The specified container does not exist.
RequestId:4d2df1c1-a01e-008b-4209-21e493000000
Time:2022-02-13T18:44:14.1140999Z
ErrorCode:ContainerNotFound
Content: <?xml version="1.0" encoding="utf-8"?><Error><Code>ContainerNotFound</Code><Message>The specified container does not exist.
RequestId:4d2df1c1-a01e-008b-4209-21e493000000
Time:2022-02-13T18:44:14.1140999Z</Message></Error>

最终,我希望能够做两件事:

  1. 将文件从我的 Blob 下载到本地计算机
  2. 将文件从我的本地计算机上传到我的 Blob。

最佳答案

错误消息是找不到容器。可能是:

  • 权限问题(连接字符串)
  • 容器名称有问题

在 MY_CONNECTION_STRING 和 MY_BLOB_CONTAINER 中,您的帐户名称和容器名称相同。如果其中之一不正确,则会解释该错误。

关于python - 如何在 Spyder/Anaconda 中使用 Python 从 Azure Blob 下载/上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71103915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com