Search code examples
pythonpython-3.xazureazure-storage

File storage in azure error handling with python


I wrote a genecric function to upload files into azure storage,, but could not figure out two things..

  1. How to check whether the upload was successful or not
  2. How to check the progress(the azure function takes a boolean parameter progress callback but I could not figure on how to use it)

Here is the function that I wrote:

def upload_files_to_azure(share, directory, path_of_file):
"""
Function to upload file to the azure storage, in a particular share into a particular directory
:param share: name of share of azure storage 
:param directory: directory name inside azure storage
:param path_of_file: the path of file to be uploaded 
:return: status of file uploaded 
"""
file_service = FileService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
file_service.create_file_from_path(
    share,
    directory,  # We want to create this blob in the root directory, so we specify None for the directory_name
    'test.sql20180103.tar.gz',
    path_of_file,
    content_settings=ContentSettings(content_type='application/x-gzip'))
return

Solution

  • 1.How to check whether the upload was successful or not.

    If you are using Azure Storage SDK , you could check if any exceptions exist. If the operation is successful, you could see your uploaded files on the portal.

    If you are using Azure Storage REST API, you could check the response message and status code.(200 is success)

    2.How to check the progress(the azure function takes a boolean parameter progress callback but I could not figure on how to use it)

    I searched the Azure Storage Python SDK and find the progress_callback arguments in the create_file_from_path method.

     progress_callback func(current, total) 
    

    You need to know the size of your file than you could check the progress of the operation. You could find the usage of it from here.

    Callback for progress with signature function(current, total) where current is the number of bytes transfered so far and total is the size of the file, or None if the total size is unknown.

    Hope it helps you.