Bug #698316: Glance reads the whole image into memory when handling a POST
/images request Change the store API so that add takes a file-like object, not a string. This avoids loading the whole request body into memory before writing it out, and allows us to stream it instead.
This commit is contained in:
@@ -223,7 +223,7 @@ class Controller(wsgi.Controller):
|
||||
|
||||
if image_in_body:
|
||||
try:
|
||||
location = store.add(image_meta['id'], req.body)
|
||||
location = store.add(image_meta['id'], req.body_file)
|
||||
except exception.Duplicate, e:
|
||||
logging.error("Error adding image to store: %s", str(e))
|
||||
return HTTPConflict(str(e), request=req)
|
||||
|
||||
@@ -110,7 +110,7 @@ class FilesystemBackend(glance.store.Backend):
|
||||
FLAGS.filesystem_store_datadir and <ID> is the supplied image ID.
|
||||
|
||||
:param id: The opaque image identifier
|
||||
:param data: The image data to write
|
||||
:param data: The image data to write, as a file-like object
|
||||
|
||||
:retval The location that was written, with file:// scheme prepended
|
||||
"""
|
||||
@@ -126,8 +126,11 @@ class FilesystemBackend(glance.store.Backend):
|
||||
raise exception.Duplicate("Image file %s already exists!"
|
||||
% filepath)
|
||||
|
||||
f = open(filepath, 'wb')
|
||||
f.write(data)
|
||||
f.close()
|
||||
with open(filepath, 'wb') as f:
|
||||
while True:
|
||||
buf = data.read(ChunkedFile.CHUNKSIZE)
|
||||
if not buf:
|
||||
break
|
||||
f.write(buf)
|
||||
|
||||
return 'file://%s' % filepath
|
||||
|
||||
Reference in New Issue
Block a user