24
24
# mappings for bucket CORS settings
25
25
BUCKET_CORS = {}
26
26
27
+ # mappings for bucket lifecycle settings
28
+ BUCKET_LIFECYCLE = {}
29
+
27
30
# set up logger
28
31
LOGGER = logging .getLogger (__name__ )
29
32
@@ -209,6 +212,30 @@ def append_cors_headers(bucket_name, request_method, request_headers, response):
209
212
break
210
213
211
214
215
+ def get_lifecycle (bucket_name ):
216
+ response = Response ()
217
+ lifecycle = BUCKET_LIFECYCLE .get (bucket_name )
218
+ if not lifecycle :
219
+ # TODO: check if bucket exists, otherwise return 404-like error
220
+ lifecycle = {
221
+ 'LifecycleConfiguration' : []
222
+ }
223
+ body = xmltodict .unparse (lifecycle )
224
+ response ._content = body
225
+ response .status_code = 200
226
+ return response
227
+
228
+
229
+ def set_lifecycle (bucket_name , lifecycle ):
230
+ # TODO: check if bucket exists, otherwise return 404-like error
231
+ if isinstance (to_str (lifecycle ), six .string_types ):
232
+ lifecycle = xmltodict .parse (lifecycle )
233
+ BUCKET_LIFECYCLE [bucket_name ] = lifecycle
234
+ response = Response ()
235
+ response .status_code = 200
236
+ return response
237
+
238
+
212
239
def strip_chunk_signatures (data ):
213
240
# For clients that use streaming v4 authentication, the request contains chunk signatures
214
241
# in the HTTP body (see example below) which we need to strip as moto cannot handle them
@@ -429,6 +456,12 @@ def forward_request(self, method, path, data, headers):
429
456
if method == 'DELETE' :
430
457
return delete_cors (bucket )
431
458
459
+ if query == 'lifecycle' or 'lifecycle' in query_map :
460
+ if method == 'GET' :
461
+ return get_lifecycle (bucket )
462
+ if method == 'PUT' :
463
+ return set_lifecycle (bucket , data )
464
+
432
465
if modified_data :
433
466
return Request (data = modified_data , headers = headers , method = method )
434
467
return True
@@ -455,7 +488,7 @@ def return_response(self, method, path, data, headers, response):
455
488
if len (path [1 :].split ('/' )[1 ]) > 0 :
456
489
parts = parsed .path [1 :].split ('/' , 1 )
457
490
# ignore bucket notification configuration requests
458
- if parsed .query != 'notification' :
491
+ if parsed .query != 'notification' and parsed . query != 'lifecycle' :
459
492
object_path = parts [1 ] if parts [1 ][0 ] == '/' else '/%s' % parts [1 ]
460
493
send_notifications (method , bucket_name , object_path )
461
494
0 commit comments