Does python has a functionality to upload tar.gz file from local pc to azure blob without extracting the files inside?

341 Views Asked by At

I have successfully downloaded tar.gz file from ftp server and have stored it in my local pc using below piece of code:

                data = BytesIO()
                save_file = ftp.retrbinary('RETR '+ filename, data.write, 1024)
                data.seek(0)
                uncompressed = gzip.decompress(data.read())
                    
                with open(filename, 'wb') as file:
                    file.write(uncompressed)
                    logging.info("success")

Now, I only want to upload the same to my azure blob storage without extracting it.
So far, I've tried this but it is letting me to do so:

```with open(filename, "rb") as f:
        blob.upload_blob(f, overwrite=True)``` 


what I am missing here?
1

There are 1 best solutions below

0
Venkatesan On

to upload tar.gz file from local pc to azure blob

I tried in my environment and got below results:

To upload tar.gz file from local folder to azure blob storage you can use below code.

Code:

from azure.storage.blob import BlobServiceClient


blobservice=BlobServiceClient.from_connection_string(conn_str="<connect-string>")
blob_client = blobservice.get_blob_client(container="test",
                                                          blob="sample1.tar.gz")
# Upload the created file
with open("C:\\Users\\v-vsettu\\Downloads\\sample.tar.gz", "rb") as data:
    blob_client.upload_blob(data)
print("Uploaded!!!!!")

Console:

enter image description here

Portal:

enter image description here

Reference: Quickstart: Azure Blob Storage client library for Python - Azure Storage | Microsoft Learn