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

ffscu2ro  于 2023-05-29  发布在  Python
关注(0)|答案(3)|浏览(152)

我是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下载文件到我的本地机器
1.从我的本地机器上传文件到我的Blob。

zf9nrax1

zf9nrax11#

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

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

在MY_CONNECTION_STRING和MY_BLOB_CONTAINER中,您的帐户名称和容器名称相同。如果其中一个是不正确的,它将解释错误。

jtoj6r0c

jtoj6r0c2#

这是我最后的工作代码。

import os
import uuid
import sys
from azure.storage.blob import BlockBlobService, PublicAccess

try:
    # Create the BlockBlobService that is used to call the Blob service for the storage account
    blob_service_client = BlockBlobService(account_name='your_account_name_goes_here', account_key='your_account_key_goes_here')
    
    # Create a container called 'quickstartblobs'.
    container_name = 'your_container_name_goes_here'
    blob_service_client.create_container(container_name)
    
    # Set the permission so the blobs are public.
    blob_service_client.set_container_acl(container_name, public_access=PublicAccess.Container)
    
    # Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download.
    local_path = os.path.expanduser('C:\\Users\\ryans\\Desktop\\blob\\')
        
    local_file_name  = 'charges.csv'
    #local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt"
    full_path_to_file = os.path.join(local_path, local_file_name)

    print("Temp file = " + full_path_to_file)
    print("\nUploading to Blob storage as blob" + local_file_name)
    
    # Upload the created file, use local_file_name for the blob name
    blob_service_client.create_blob_from_path(container_name, local_file_name, full_path_to_file)
    
    # List the blobs in the container
    print("\nList blobs in the container")
    generator = blob_service_client.list_blobs(container_name)
    
    for blob in generator:
        print("\t Blob name: " + blob.name)

except Exception as e:
    print(e)
    
##############################################################################################

import os
import uuid
import sys
from azure.storage.blob import BlockBlobService, PublicAccess

try:
    
    local_path = os.path.expanduser('C:\\Users\\ryans\\Desktop\\blob\\')
    container_name = 'your_container_name_goes_here'
    local_file_name  = 'charges.csv'
    
    # Download the blob(s).
    # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
    full_path_to_file = os.path.join(local_path, local_file_name)
    print("\nDownloading blob to " + full_path_to_file)
    blob_service_client.get_blob_to_path(container_name, local_file_name, full_path_to_file)
    
    #sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample application will exit.")
    sys.stdout.flush()
    input()

except Exception as e:
    print(e)

这个链接对我来说是一个很好的资源。
https://github.com/Azure-Samples/azure-sdk-for-python-storage-blob-upload-download/blob/master/example.py

ftf50wuq

ftf50wuq3#

如果有人在寻找最新的答案/其他选择:

# pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient

def download_blob(storage_connection_string, container_name, blob_name):
    blob_service_client = BlobServiceClient.from_connection_string(storage_connection_string)
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    
    with open(blob_name, "wb") as file:
        file.write(blob_client.download_blob().readall())

# Set the connection string, container name, blob name
storage_connection_string = "<your connection string which you can get from access keys section from container in storage account>"
container_name = "<container name>" # container in which file is being stored
blob_name = "<file name which is there as blob inside the container>" # file which you need to download

# Download the blob
download_blob(storage_connection_string, container_name, blob_name)

相关问题