Upload image to azure blob storage using python
Asked Answered
S

6

6

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:

  1. Even after installing azure-blob-storage, there is no such thing as BlobService in that package.
  2. Is there any place where it is clearly documented how to do this?
Shower answered 6/9, 2019 at 6:34 Comment(0)
G
4

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.

enter image description here

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/.

  1. $ virtualenv test
  2. $ cd test
  3. $ Scripts\active
  4. $ pip install azure-storage

Then, you can run my sample code via python upload_images.py in the current Python virtual environment.

Grimalkin answered 6/9, 2019 at 7:30 Comment(2)
The thing is that even after install azure-blob-storage, there is no BlockBlobService in this package.Shower
@mlRocks Please init the Python virtual env and install the azure-storage package. I have updated my post.Grimalkin
C
6

It's in the documentation that you linked.

It's not BlobService, it is BlobClient class.

from azure.storage.blob import BlobClient

blob_client = BlobClient.from_connection_string(
        conn_str='my_conn_str',
        container_name='my_container_name',
        blob_name='my_blob_name')

with open("./SampleSource.txt", "rb") as data:
    blob.upload_blob(data)

See the BlobClient.from_connection_string documentation.

Cheesecake answered 6/9, 2019 at 7:27 Comment(0)
G
4

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.

enter image description here

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/.

  1. $ virtualenv test
  2. $ cd test
  3. $ Scripts\active
  4. $ pip install azure-storage

Then, you can run my sample code via python upload_images.py in the current Python virtual environment.

Grimalkin answered 6/9, 2019 at 7:30 Comment(2)
The thing is that even after install azure-blob-storage, there is no BlockBlobService in this package.Shower
@mlRocks Please init the Python virtual env and install the azure-storage package. I have updated my post.Grimalkin
A
1

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 )
Ashby answered 18/9, 2020 at 12:5 Comment(0)
S
0

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://learn.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python

[2] https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.azureclicredential?view=azure-python

Sublet answered 7/11, 2020 at 13:55 Comment(0)
G
0
def upload_file(remote_path,local_path):
    try:
        blobService = BlockBlobService(account_name=SETTINGS.AZURE_ACCOUNT_NAME, account_key=SETTINGS.AZURE_ACCOUNT_KEY)
        blobService.create_blob_from_path('data',remote_path,local_path)
    except Exception as e:
        logger.error(f'Unable to save azure blob data. {str(e)}')
        raise Exception(f'Unable to save azure blob data. {str(e)}')
Giro answered 30/11, 2021 at 8:14 Comment(0)
S
0
from azure.storage.blob import BlobClient,ContentSettings

blob = BlobClient.from_connection_string(conn_str=connection_string, container_name=container_name, blob_name="my_blob9")
image_content_setting = ContentSettings(content_type='image/jpeg')

with open("/content/sample_data/kanha.png","rb") as data:
    blob.upload_blob(data,overwrite=True,content_settings=image_content_setting)
Sera answered 12/10, 2022 at 10:38 Comment(1)
if you want to upload as png/jpeg/original file then you can upload using this code else for binary you can check https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob#create-a-containerSera

© 2022 - 2024 — McMap. All rights reserved.