Allow writing empty body to s3 key if content-length is zero. Better fix for #6.
This commit is contained in:
parent
756955b61e
commit
e64c73efed
@ -80,19 +80,17 @@ def key_response(uri_info, method, body, headers):
|
|||||||
s3_backend.copy_key(src_bucket, src_key, bucket_name, key_name)
|
s3_backend.copy_key(src_bucket, src_key, bucket_name, key_name)
|
||||||
template = Template(S3_OBJECT_COPY_RESPONSE)
|
template = Template(S3_OBJECT_COPY_RESPONSE)
|
||||||
return template.render(key=src_key)
|
return template.render(key=src_key)
|
||||||
if body is not None:
|
content_length = int(headers.get('Content-Length', 0))
|
||||||
key = s3_backend.get_key(bucket_name, key_name)
|
if body or (body == '' and content_length == 0):
|
||||||
if not key or body:
|
# We want to write the key in once of two circumstances.
|
||||||
# We want to write the key in once of two circumstances.
|
# - Anytime we are given a truthy body value
|
||||||
# - The key does not currently exist.
|
# - We are given an empty body value and the content length is zero.
|
||||||
# - The key already exists, but body is a truthy value.
|
# The reason we do not set the key to an empty string if the
|
||||||
# This allows us to write empty strings to keys for the first
|
# content length is not zero is because we are sometimes sent an
|
||||||
# write, but not subsequent. This is because HTTPretty sends
|
# empty string as part of closing the connection.
|
||||||
# an empty string on connection close. This is a temporary fix
|
new_key = s3_backend.set_key(bucket_name, key_name, body)
|
||||||
# while HTTPretty gets fixed.
|
template = Template(S3_OBJECT_RESPONSE)
|
||||||
new_key = s3_backend.set_key(bucket_name, key_name, body)
|
return template.render(key=new_key), dict(etag=new_key.etag)
|
||||||
template = Template(S3_OBJECT_RESPONSE)
|
|
||||||
return template.render(key=new_key), dict(etag=new_key.etag)
|
|
||||||
key = s3_backend.get_key(bucket_name, key_name)
|
key = s3_backend.get_key(bucket_name, key_name)
|
||||||
if key:
|
if key:
|
||||||
return "", dict(etag=key.etag)
|
return "", dict(etag=key.etag)
|
||||||
|
@ -62,6 +62,20 @@ def test_empty_key():
|
|||||||
bucket.get_key("the-key").get_contents_as_string().should.equal('')
|
bucket.get_key("the-key").get_contents_as_string().should.equal('')
|
||||||
|
|
||||||
|
|
||||||
|
@mock_s3
|
||||||
|
def test_empty_key_set_on_existing_key():
|
||||||
|
conn = boto.connect_s3('the_key', 'the_secret')
|
||||||
|
bucket = conn.create_bucket("foobar")
|
||||||
|
key = Key(bucket)
|
||||||
|
key.key = "the-key"
|
||||||
|
key.set_contents_from_string("foobar")
|
||||||
|
|
||||||
|
bucket.get_key("the-key").get_contents_as_string().should.equal('foobar')
|
||||||
|
|
||||||
|
key.set_contents_from_string("")
|
||||||
|
bucket.get_key("the-key").get_contents_as_string().should.equal('')
|
||||||
|
|
||||||
|
|
||||||
@mock_s3
|
@mock_s3
|
||||||
def test_copy_key():
|
def test_copy_key():
|
||||||
conn = boto.connect_s3('the_key', 'the_secret')
|
conn = boto.connect_s3('the_key', 'the_secret')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user