I have a set of files (which are not locally saved) that needs to be uploaded onto azure blob storage and updated everyday.
(1) There are certain number of files with same name (with different contents) which should be saved as individual blobs.
(2) The updated set of files should overwrite the respective previous day blobs.
Is there a way to check if blob already exists and dynamically rename it by appending a number (can't append timestamp because of (2))?
I am using the below function to upload all my files:
def azure_upload_file(block_blob_service, container, local_file_path, local_file_name):
logger = logging.getLogger('data')
isExist = block_blob_service.exists(container, local_file_name)
blobname = os.path.splitext(local_file_name)[0]
blobext = os.path.splitext(local_file_name)[1]
if isExist is True:
blob_file_name = '{}_{}{}'.format(blobname, '#', blobext)
else:
blob_file_name = local_file_name
full_path_to_file =os.path.join(local_file_path, local_file_name)
blob = block_blob_service.create_blob_from_path(container, blob_file_name, full_path_to_file)
blob_url = block_blob_service.make_blob_url(container, blob_file_name)
logger.info('Uploaded file {} to azure blob storage'.format(blob_file_name))
os.unlink(full_path_to_file)
return blob_url
Example:
Date: 19-11-2019 - Initial Upload
filename.ext -> blob
1. abcd.zip -> abcd.zip
2. abcd.zip -> abcd(1).zip
3. abcd.zip -> abcd(2).zip
4. defg.csv -> defg.csv
and so on..
All I want is to somehow fill the '#' in the code intelligently such that whenever I have the updated set of files, I would already know to which blob I should overwrite the file to.
i.e., if I have a new set of files on 20-11-2019
Example:
Date: 20-11-2019 - Second Upload
new filename.ext -> blob
1. abcd.zip -> abcd.zip
2. abcd.zip -> abcd(1).zip
3. abcd.zip -> abcd(2).zip
4. defg.csv -> defg.csv
and so on..
I have already gone through similar articles:
1. Azure blob upload rename if blob name exist
2. Faster Azure blob name search with python?
Both of them don't solve my problem. Wondering if there is an efficient and easy way this can be achieved?