我试图从运行python代码的Azure函数中将文件输出到存储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()时没有多大意义。但是现在我不知道下一步该做什么。
我正在使用Python2.7。
有什么建议吗?
编辑
对我所经历的确切错误的进一步澄清:
在使用时
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)我能够完成函数脚本,但是显示在Azure存储blob中的zip文件的大小只有几个字节,并且已经损坏。仍然在我的webjobs存储上的zip文件实际上大约有250 it,而且我能够从它中将文件提取回我的webstorage。
因此,我的错误很可能来源于我的输出代码:
RFile = open('Data_out.zip', 'r').read()
output = open(os.environ['returnfile'], 'w')
output.write(RFile)发布于 2017-06-02 10:33:01
因此,经过更多的测试,我找到了解决问题的方法。我转向python寻找天蓝色的存储。这给了我更多的控制力。
我使用KUDU安装天蓝色存储包(必须升级PIP才能正确安装),并将包导入脚本中,并附加引用“/env/Lib/site- package”,如下所示:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'env/Lib/site-packages')))
from azure.storage.blob import BlockBlobService然后,我的输出方法与在:https://learn.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')就这样了!
https://stackoverflow.com/questions/44260265
复制相似问题