Add unpack_zip_archive_in_memory func.
The purpose of the func:
unpacking zip archive in Ram, without using hard drive
memory.
The func will be called from the upload hook of
both ns_package artifact and hooks_artifact.
Change-Id: Id4287a3f56475540f58e2728e0333612077e931a
(cherry picked from commit 6d8356b0fb
)
This commit is contained in:
parent
b3d8db07ec
commit
dd61d8fc53
|
@ -15,6 +15,7 @@
|
|||
|
||||
"""Contains additional file utils that may be useful for upload hooks."""
|
||||
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
import zipfile
|
||||
|
@ -24,6 +25,7 @@ from oslo_log import log as logging
|
|||
from oslo_utils import excutils
|
||||
|
||||
from glare.common import store_api
|
||||
from glare.common import utils
|
||||
from glare.objects.meta import fields as glare_fields
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
@ -98,3 +100,28 @@ def upload_content_file(context, af, data, blob_dict, key_name,
|
|||
blob.update(checksums)
|
||||
getattr(af, blob_dict)[key_name] = blob
|
||||
af.update_blob(context, af.id, blob_dict, getattr(af, blob_dict))
|
||||
|
||||
|
||||
def unpack_zip_archive_in_memory(context, af, fd):
|
||||
"""Unpack zip archive in memory.
|
||||
|
||||
:param context: user context
|
||||
:param af: artifact object
|
||||
:param fd: file
|
||||
:return io.BytesIO object - simple stream of in-memory bytes, None
|
||||
"""
|
||||
flobj = io.BytesIO(fd.read())
|
||||
while True:
|
||||
data = fd.read(65536)
|
||||
if data == b'': # end of file reached
|
||||
break
|
||||
flobj.write(data)
|
||||
|
||||
zip_ref = zipfile.ZipFile(flobj, 'r')
|
||||
for name in zip_ref.namelist():
|
||||
if not name.endswith('/'):
|
||||
upload_content_file(
|
||||
context, af, utils.BlobIterator(zip_ref.read(name)),
|
||||
'content', name)
|
||||
flobj.seek(0)
|
||||
return flobj, None
|
||||
|
|
Loading…
Reference in New Issue