Added support for metadata on files, and support for POST:ing files to S3
This commit is contained in:
parent
755fe6563b
commit
3880be5ea9
@ -10,6 +10,13 @@ class FakeKey(object):
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.value = value
|
self.value = value
|
||||||
self.last_modified = datetime.datetime.now()
|
self.last_modified = datetime.datetime.now()
|
||||||
|
self._metadata = {}
|
||||||
|
|
||||||
|
def set_metadata(self, key, metadata):
|
||||||
|
self._metadata[key] = metadata
|
||||||
|
|
||||||
|
def get_metadata(self, key):
|
||||||
|
return self._metadata[key]
|
||||||
|
|
||||||
def append_to_value(self, value):
|
def append_to_value(self, value):
|
||||||
self.value += value
|
self.value += value
|
||||||
@ -32,6 +39,10 @@ class FakeKey(object):
|
|||||||
RFC1123 = '%a, %d %b %Y %H:%M:%S GMT'
|
RFC1123 = '%a, %d %b %Y %H:%M:%S GMT'
|
||||||
return self.last_modified.strftime(RFC1123)
|
return self.last_modified.strftime(RFC1123)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def metadata(self):
|
||||||
|
return self._metadata
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def response_dict(self):
|
def response_dict(self):
|
||||||
return {
|
return {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from urlparse import parse_qs, urlparse
|
from urlparse import parse_qs, urlparse
|
||||||
|
import re
|
||||||
|
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
|
|
||||||
@ -67,6 +68,22 @@ def _bucket_response(request, full_url, headers):
|
|||||||
# Tried to delete a bucket that still has keys
|
# Tried to delete a bucket that still has keys
|
||||||
template = Template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR)
|
template = Template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR)
|
||||||
return 409, headers, template.render(bucket=removed_bucket)
|
return 409, headers, template.render(bucket=removed_bucket)
|
||||||
|
elif method == 'POST':
|
||||||
|
#POST to bucket-url should create file from form
|
||||||
|
key = request.form['key']
|
||||||
|
f = request.form['file']
|
||||||
|
new_key = s3_backend.set_key(bucket_name, key, "")
|
||||||
|
#TODO Set actual file
|
||||||
|
|
||||||
|
#Metadata
|
||||||
|
meta_regex = re.compile('^x-amz-meta-([a-zA-Z0-9\-_]+)$', flags=re.IGNORECASE)
|
||||||
|
for form_id in request.form:
|
||||||
|
result = meta_regex.match(form_id)
|
||||||
|
if result:
|
||||||
|
meta_key = result.group(0).lower()
|
||||||
|
metadata = request.form[form_id]
|
||||||
|
new_key.set_metadata(meta_key, metadata)
|
||||||
|
return 200, headers, ""
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Method {} has not been impelemented in the S3 backend yet".format(method))
|
raise NotImplementedError("Method {} has not been impelemented in the S3 backend yet".format(method))
|
||||||
|
|
||||||
@ -84,8 +101,8 @@ def _key_response(request, full_url, headers):
|
|||||||
parsed_url = urlparse(full_url)
|
parsed_url = urlparse(full_url)
|
||||||
method = request.method
|
method = request.method
|
||||||
|
|
||||||
key_name = parsed_url.path.lstrip('/')
|
|
||||||
bucket_name = bucket_name_from_url(full_url)
|
bucket_name = bucket_name_from_url(full_url)
|
||||||
|
key_name = parsed_url.path.split(bucket_name + '/')[-1]
|
||||||
if hasattr(request, 'body'):
|
if hasattr(request, 'body'):
|
||||||
# Boto
|
# Boto
|
||||||
body = request.body
|
body = request.body
|
||||||
@ -96,7 +113,8 @@ def _key_response(request, full_url, headers):
|
|||||||
if method == 'GET':
|
if method == 'GET':
|
||||||
key = s3_backend.get_key(bucket_name, key_name)
|
key = s3_backend.get_key(bucket_name, key_name)
|
||||||
if key:
|
if key:
|
||||||
return key.value
|
headers.update(key.metadata)
|
||||||
|
return 200, headers, key.value
|
||||||
else:
|
else:
|
||||||
return 404, headers, ""
|
return 404, headers, ""
|
||||||
if method == 'PUT':
|
if method == 'PUT':
|
||||||
@ -118,6 +136,15 @@ def _key_response(request, full_url, headers):
|
|||||||
# Initial data
|
# Initial data
|
||||||
new_key = s3_backend.set_key(bucket_name, key_name, body)
|
new_key = s3_backend.set_key(bucket_name, key_name, body)
|
||||||
request.streaming = True
|
request.streaming = True
|
||||||
|
|
||||||
|
#Metadata
|
||||||
|
meta_regex = re.compile('^x-amz-meta-([a-zA-Z0-9\-_]+)$', flags=re.IGNORECASE)
|
||||||
|
for header in request.headers:
|
||||||
|
result = meta_regex.match(header[0])
|
||||||
|
if result:
|
||||||
|
meta_key = result.group(0).lower()
|
||||||
|
metadata = header[1]
|
||||||
|
new_key.set_metadata(meta_key, metadata)
|
||||||
template = Template(S3_OBJECT_RESPONSE)
|
template = Template(S3_OBJECT_RESPONSE)
|
||||||
headers.update(new_key.response_dict)
|
headers.update(new_key.response_dict)
|
||||||
return 200, headers, template.render(key=new_key)
|
return 200, headers, template.render(key=new_key)
|
||||||
@ -125,7 +152,7 @@ def _key_response(request, full_url, headers):
|
|||||||
key = s3_backend.get_key(bucket_name, key_name)
|
key = s3_backend.get_key(bucket_name, key_name)
|
||||||
if key:
|
if key:
|
||||||
headers.update(key.response_dict)
|
headers.update(key.response_dict)
|
||||||
return 200, headers, S3_OBJECT_RESPONSE
|
return 200, headers, ""
|
||||||
else:
|
else:
|
||||||
return 404, headers, ""
|
return 404, headers, ""
|
||||||
elif method == 'DELETE':
|
elif method == 'DELETE':
|
||||||
|
Loading…
Reference in New Issue
Block a user