问题
I have an image directory named images
which contains image files as:
images
--0001.png
--0002.jpg
--0003.png
Now I want to upload this directory to my azure blob storage with the same file structure. I looked at the sample code given here and here but:
- Even after installing
azure-blob-storage
, there is no such thing asBlobService
in that package. - Is there any place where it is clearly documented how to do this?
回答1:
Here is my sample code works fine for me.
import os
from azure.storage.blob import BlockBlobService
root_path = '<your root path>'
dir_name = 'images'
path = f"{root_path}/{dir_name}"
file_names = os.listdir(path)
account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name, such as `test` for me>'
block_blob_service = BlockBlobService(
account_name=account_name,
account_key=account_key
)
for file_name in file_names:
blob_name = f"{dir_name}/{file_name}"
file_path = f"{path}/{file_name}"
block_blob_service.create_blob_from_path(container_name, blob_name, file_path)
The result as the figure below be screenshot from Azure Storage Explorer.
For more details about API references of Azure Storage SDK for Python, please refer to https://azure-storage.readthedocs.io/index.html.
Update: My used Python version is Python 3.7.4 on Windows, and the required package is azure-storage==0.36.0
, you can find it from https://pypi.org/project/azure-storage/.
$ virtualenv test
$ cd test
$ Scripts\active
$ pip install azure-storage
Then, you can run my sample code via python upload_images.py
in the current Python virtual environment.
回答2:
It's in the documentation you linked.
It's not BlobService it is BlobClient.
from azure.storage.blob import BlobClient
blob = BlobClient.from_connection_string("my_connection_string", container="mycontainer", blob="my_blob")
with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)
回答3:
Currently there are two versions of azure.storage.blob . If you create a Azure VM and process data there, you can end up with either one of them.
The older version requires (as pointed out by Adam Marczak):
from azure.storage.blob import BlobClient
blob = BlobClient.from_connection_string("my_connection_string", container="mycontainer", blob="my_blob")
with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)
while the newer:
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(account_name, account_key)
blob_service.create_blob_from_path(
container_name, blob_name , full_file_path )
回答4:
Apparently, neither create_blob_from_bytes nor BlockBlobService exists in the latest python SDK[1] which you get by default unless you are managing the versions of various packages.
I am assuming you might want to execute this as a standalone script. So you can authenticate using AzureCliCredential[2] and retrieve the necessary resource endpoints via the methods provided in the SDK.
Below code won't work in the Azure Functions.
from azure.storage.blob import (
BlobServiceClient,
ContentSettings
)
storage_connection_string='DefaultEndpointsProtocol=https;AccountName=<STORAGE_ACCOUNT_NAME>;AccountKey=<ACCOUNT_KEY>;EndpointSuffix=core.windows.net'
container_name =
blob_service_client = BlobServiceClient.(
conn_str=storage_connection_string
)
logging.debug(f'getting client for container : {container_name}')
container_client =
blob_service_client.get_container_client(container=container_name)
blob_client = container_client.get_blob_client(blob_name)
if blob_client.exists():
blob_client.delete_blob()
blob_client =blob_service_client.get_blob_client(container=container_name,
blob=blob_name)
try:
with open(filename, "rb") as data:
blob.upload(data)
content_settings =ContentSettings(content_type='image/png')
logging.debug(f'setting the content type : {content_settings}')
except Exception as e:
logging.error(str(e))
[1] https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python
[2] https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity.azureclicredential?view=azure-python
来源:https://stackoverflow.com/questions/57816849/upload-image-to-azure-blob-storage-using-python