Azure Function - Unzip Password Protected File Using Python Code
I am trying to unzip password protected file which is stored on Azure Blob container. I want to extract it on Azure Blob itself. I have created a Azure Function App using Python (c
Solution 1:
The zip files are stored in Azure blob storage server. It is a remote server. We cannot use ZipFile(blob.name)
to access it. We need to read the zip file's content at first then unzip it.
For example
blob_service_client = BlobServiceClient.from_connection_string(conn_str)
container_client = blob_service_client.get_container_client('input')
blob_client = container_client.get_blob_client('sampleData.zip')
des_container_client = blob_service_client.get_container_client('output')
with io.BytesIO() as b:
download_stream = blob_client.download_blob(0)
download_stream.readinto(b)
with zipfile.ZipFile(b, compression=zipfile.ZIP_LZMA) as z:
for filename in z.namelist():
ifnot filename.endswith('/'):
print(filename)
with z.open(filename, mode='r', pwd=b'') as f:
des_container_client.get_blob_client(
filename).upload_blob(f)
Solution 2:
ZipFile(blob.name)
is looking for the file in local file system.
You need to download the blob to local file system.
# Download blob to local file
local_file_path = os.path.join(".", blob.name)
withopen(local_file_path, "wb") as download_file:
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
download_file.write(blob_client.download_blob().readall())
# read local zip filewith ZipFile(local_file_path) as zf:
zf.extractall(pwd=b'password')
Post a Comment for "Azure Function - Unzip Password Protected File Using Python Code"