Azure Function + Python - 输出压缩的 zipfile
Azure Function + Python - Output compressed zipfile
我正在尝试从我的 Azure Function 运行 python 代码将文件输出到我的存储 blob。我已经使用以下代码完成了没有任何压缩的返回文件:
with zipfile.ZipFile('Data_out.zip', 'w') as myzip:
myzip.write('somefile.js')
print 'adding somefile.js'
和
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
但是,一旦我开始使用任何形式的压缩并将其读回到输出绑定中,复制到我的存储 blob 的文件就会损坏且无法读取。
import zipfile
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
modes = {zipfile.ZIP_DEFLATED: 'deflated',
zipfile.ZIP_STORED: 'stored',
}
print 'creating archive'
zf = zipfile.ZipFile('Data_out.zip', mode='w')
try:
print 'adding log.txt and outputfile with compression mode', modes[compression]
zf.write('log.txt', compress_type=compression)
zf.write('somefile.js', compress_type=compression)
finally:
print 'closing'
zf.close()
和
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
现在这会在我的 webjobs 文件夹中生成一个功能齐全的 zip 文件。但我无法将其正确复制到我的存储 blob。我的猜测是,在处理压缩文件时,将 .read() 与 .write() 一起使用没有多大意义。但此刻我不知道下一步该怎么做。
我正在使用 Python 2.7.
有什么建议吗?
编辑
进一步澄清我遇到的确切错误:
使用时
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
我能够完成函数脚本,但出现在我的 Azure 存储 blob 中的 zip 文件只有几个字节大小并且已损坏。仍在我的 webjobs 存储中的 zip 文件实际上约为 250kb,我可以从中将文件提取回我的 webstorage。
所以我的错误来源很可能在我的输出代码中:
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
经过更多测试后,我找到了解决问题的方法。我转而使用 python SDK 进行 azure-storage。这为我提供了更多控制权。
我使用 KUDU 安装了 azure-storage 包(必须升级 PIP 才能正确安装),然后我将该包导入到我的脚本中,并附加了对“/env/Lib/site-packages”的引用,如下所示:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'env/Lib/site-packages')))
from azure.storage.blob import BlockBlobService
然后我的输出方法与解释的相同:https://docs.microsoft.com/en-us/azure/storage/storage-python-how-to-use-blob-storage .
代码最终是这样的:
block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')
block_blob_service.create_container(username)
block_blob_service.create_blob_from_path(
username,
returnfile,
'Data_out.zip')
就是这样!
我正在尝试从我的 Azure Function 运行 python 代码将文件输出到我的存储 blob。我已经使用以下代码完成了没有任何压缩的返回文件:
with zipfile.ZipFile('Data_out.zip', 'w') as myzip:
myzip.write('somefile.js')
print 'adding somefile.js'
和
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
但是,一旦我开始使用任何形式的压缩并将其读回到输出绑定中,复制到我的存储 blob 的文件就会损坏且无法读取。
import zipfile
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
modes = {zipfile.ZIP_DEFLATED: 'deflated',
zipfile.ZIP_STORED: 'stored',
}
print 'creating archive'
zf = zipfile.ZipFile('Data_out.zip', mode='w')
try:
print 'adding log.txt and outputfile with compression mode', modes[compression]
zf.write('log.txt', compress_type=compression)
zf.write('somefile.js', compress_type=compression)
finally:
print 'closing'
zf.close()
和
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
现在这会在我的 webjobs 文件夹中生成一个功能齐全的 zip 文件。但我无法将其正确复制到我的存储 blob。我的猜测是,在处理压缩文件时,将 .read() 与 .write() 一起使用没有多大意义。但此刻我不知道下一步该怎么做。
我正在使用 Python 2.7.
有什么建议吗?
编辑
进一步澄清我遇到的确切错误:
使用时
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
我能够完成函数脚本,但出现在我的 Azure 存储 blob 中的 zip 文件只有几个字节大小并且已损坏。仍在我的 webjobs 存储中的 zip 文件实际上约为 250kb,我可以从中将文件提取回我的 webstorage。
所以我的错误来源很可能在我的输出代码中:
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)
经过更多测试后,我找到了解决问题的方法。我转而使用 python SDK 进行 azure-storage。这为我提供了更多控制权。
我使用 KUDU 安装了 azure-storage 包(必须升级 PIP 才能正确安装),然后我将该包导入到我的脚本中,并附加了对“/env/Lib/site-packages”的引用,如下所示:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'env/Lib/site-packages')))
from azure.storage.blob import BlockBlobService
然后我的输出方法与解释的相同:https://docs.microsoft.com/en-us/azure/storage/storage-python-how-to-use-blob-storage .
代码最终是这样的:
block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')
block_blob_service.create_container(username)
block_blob_service.create_blob_from_path(
username,
returnfile,
'Data_out.zip')
就是这样!