Browse code

Add and remove full lifecycle policies

Adds functions for creating and removing full XML lifecycle policies, this will enable use of things like Glacier transition and multiple prefixes and rules. Mostly copied from the add and remove policy functions.

Sam Rudge authored on 2014/06/04 00:53:08
Showing 2 changed files
... ...
@@ -685,6 +685,23 @@ class S3(object):
685 685
         response = self.send_request(request)
686 686
         return response
687 687
 
688
+    def set_lifecycle_policy(self, uri, policy):
689
+        headers = SortedDict(ignore_case = True)
690
+        headers['content-md5'] = compute_content_md5(policy)
691
+        request = self.create_request("BUCKET_CREATE", uri = uri,
692
+                                      extra = "?lifecycle", headers=headers)
693
+        body = policy
694
+        debug(u"set_lifecycle_policy(%s): policy-xml: %s" % (uri, body))
695
+        request.sign()
696
+        response = self.send_request(request, body=body)
697
+        return response
698
+
699
+    def delete_lifecycle_policy(self, uri):
700
+        request = self.create_request("BUCKET_DELETE", uri = uri, extra = "?lifecycle")
701
+        debug(u"delete_lifecycle_policy(%s)" % uri)
702
+        response = self.send_request(request)
703
+        return response
704
+
688 705
     def get_multipart(self, uri):
689 706
         request = self.create_request("BUCKET_LIST", bucket = uri.bucket(), extra = "?uploads")
690 707
         response = self.send_request(request)
... ...
@@ -276,6 +276,34 @@ def cmd_expiration_set(args):
276 276
             raise
277 277
     return EX_OK
278 278
 
279
+def cmd_setlifecyclepolicy(args):
280
+    s3 = S3(cfg)
281
+    uri = S3Uri(args[1])
282
+    lifecycle_policy_file = args[0]
283
+    lifecycle_policy = open(lifecycle_policy_file, 'r').read()
284
+
285
+    if cfg.dry_run: return EX_OK
286
+
287
+    response = s3.set_lifecycle_policy(uri, lifecycle_policy)
288
+
289
+    #if retsponse['status'] == 200:
290
+    debug(u"response - %s" % response['status'])
291
+    if response['status'] == 204:
292
+        output(u"%s: Lifecycle policy updated" % uri)
293
+    return EX_OK
294
+
295
+def cmd_delpolicy(args):
296
+    s3 = S3(cfg)
297
+    uri = S3Uri(args[0])
298
+    if cfg.dry_run: return EX_OK
299
+
300
+    response = s3.delete_lifecycle_policy(uri)
301
+
302
+    #if retsponse['status'] == 200:
303
+    debug(u"response - %s" % response['status'])
304
+    output(u"%s: Lifecycle policy deleted" % uri)
305
+    return EX_OK
306
+
279 307
 def cmd_bucket_delete(args):
280 308
     def _bucket_delete_one(uri):
281 309
         try:
... ...
@@ -1547,6 +1575,34 @@ def cmd_delpolicy(args):
1547 1547
     output(u"%s: Policy deleted" % uri)
1548 1548
     return EX_OK
1549 1549
 
1550
+def cmd_setlifecycle(args):
1551
+    s3 = S3(cfg)
1552
+    uri = S3Uri(args[1])
1553
+    lifecycle_policy_file = args[0]
1554
+    lifecycle_policy = open(lifecycle_policy_file, 'r').read()
1555
+
1556
+    if cfg.dry_run: return EX_OK
1557
+
1558
+    response = s3.set_lifecycle_policy(uri, lifecycle_policy)
1559
+
1560
+    #if retsponse['status'] == 200:
1561
+    debug(u"response - %s" % response['status'])
1562
+    if response['status'] == 204:
1563
+        output(u"%s: Lifecycle Policy updated" % uri)
1564
+    return EX_OK
1565
+
1566
+def cmd_dellifecycle(args):
1567
+    s3 = S3(cfg)
1568
+    uri = S3Uri(args[0])
1569
+    if cfg.dry_run: return EX_OK
1570
+
1571
+    response = s3.delete_lifecycle_policy(uri)
1572
+
1573
+    #if retsponse['status'] == 200:
1574
+    debug(u"response - %s" % response['status'])
1575
+    output(u"%s: Lifecycle Policy deleted" % uri)
1576
+    return EX_OK
1577
+
1550 1578
 def cmd_multipart(args):
1551 1579
     s3 = S3(cfg)
1552 1580
     uri = S3Uri(args[0])
... ...
@@ -1979,6 +2035,8 @@ def get_commands_list():
1979 1979
 
1980 1980
     ## Lifecycle commands
1981 1981
     {"cmd":"expire", "label":"Set or delete expiration rule for the bucket", "param":"s3://BUCKET", "func":cmd_expiration_set, "argc":1},
1982
+    {"cmd":"setlifecycle", "label":"Upload a lifecycle policy for the bucket", "param":"s3://BUCKET", "func":cmd_setlifecycle, "argc":1},
1983
+    {"cmd":"dellifecycle", "label":"Remove a lifecycle policy for the bucket", "param":"s3://BUCKET", "func":cmd_dellifecycle, "argc":1},
1982 1984
 
1983 1985
     ## CloudFront commands
1984 1986
     {"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0},