我只是想知道是否有一种方法可以从Azure存储中提取受密码保护的zip文件。我尝试使用python Azure函数无济于事,但在读取文件位置时遇到了问题。
该文件是否必须暂时存储在共享位置上才能实现?
只是在这里寻找方向,我是否错过了一步?
问候,詹姆斯
Azure blob存储仅提供存储功能,没有正在运行的env可以执行解压缩操作。因此,基本上,我们应该将.zip文件下载到Azure函数,将其解压缩,然后按1上载到.zip文件中的文件。
为了进行快速测试,我编写了一个HTTP触发器Azure功能演示,该示例演示了使用密码保护的zip文件解压缩,它在local上对我有用:
import azure.functions as func
import uuid
import os
import shutil
from azure.storage.blob import ContainerClient
from zipfile import ZipFile
storageAccountConnstr = '<storage account conn str>'
container = '<container name>'
#define local temp path, on Azure, the path is recommanded under /home
tempPathRoot = 'd:/temp/'
unZipTempPathRoot = 'd:/unZipTemp/'
def main(req=func.HttpRequest) -> func.HttpResponse:
reqBody = req.get_json()
fileName = reqBody['fileName']
zipPass = reqBody['password']
container_client = ContainerClient.from_connection_string(storageAccountConnstr,container)
#download zip file
zipFilePath = tempPathRoot + fileName
with open(zipFilePath, "wb") as my_blob:
download_stream = container_client.get_blob_client(fileName).download_blob()
my_blob.write(download_stream.readall())
#unzip to temp folder
unZipTempPath = unZipTempPathRoot + str(uuid.uuid4())
with ZipFile(zipFilePath) as zf:
zf.extractall(path=unZipTempPath,pwd=bytes(zipPass,'utf8'))
#upload all files in temp folder
for root, dirs, files in os.walk(unZipTempPath):
for file in files:
filePath = os.path.join(root, file)
destBlobClient = container_client.get_blob_client(fileName + filePath.replace(unZipTempPath,''))
with open(filePath, "rb") as data:
destBlobClient.upload_blob(data,overwrite=True)
#remove all temp files
shutil.rmtree(unZipTempPath)
os.remove(zipFilePath)
return func.HttpResponse("done")
使用blob触发器会更好,因为如果zip文件的大小很大,则会导致超时错误。
无论如何,这只是一个演示,向您展示如何执行此操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句