OLD | NEW |
1 # Copyright 2010 Google Inc. | 1 # Copyright 2010 Google Inc. |
2 # | 2 # |
3 # Permission is hereby granted, free of charge, to any person obtaining a | 3 # Permission is hereby granted, free of charge, to any person obtaining a |
4 # copy of this software and associated documentation files (the | 4 # copy of this software and associated documentation files (the |
5 # "Software"), to deal in the Software without restriction, including | 5 # "Software"), to deal in the Software without restriction, including |
6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
7 # tribute, sublicense, and/or sell copies of the Software, and to permit | 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
8 # persons to whom the Software is furnished to do so, subject to the fol- | 8 # persons to whom the Software is furnished to do so, subject to the fol- |
9 # lowing conditions: | 9 # lowing conditions: |
10 # | 10 # |
(...skipping 11 matching lines...) Expand all Loading... |
22 import boto | 22 import boto |
23 from boto import handler | 23 from boto import handler |
24 from boto.exception import InvalidAclError | 24 from boto.exception import InvalidAclError |
25 from boto.gs.acl import ACL, CannedACLStrings | 25 from boto.gs.acl import ACL, CannedACLStrings |
26 from boto.gs.acl import SupportedPermissions as GSPermissions | 26 from boto.gs.acl import SupportedPermissions as GSPermissions |
27 from boto.gs.key import Key as GSKey | 27 from boto.gs.key import Key as GSKey |
28 from boto.s3.acl import Policy | 28 from boto.s3.acl import Policy |
29 from boto.s3.bucket import Bucket as S3Bucket | 29 from boto.s3.bucket import Bucket as S3Bucket |
30 import xml.sax | 30 import xml.sax |
31 | 31 |
| 32 # constants for default object ACL and standard acl in http query args |
| 33 DEF_OBJ_ACL = 'defaultObjectAcl' |
| 34 STANDARD_ACL = 'acl' |
| 35 |
32 class Bucket(S3Bucket): | 36 class Bucket(S3Bucket): |
33 | 37 |
34 def __init__(self, connection=None, name=None, key_class=GSKey): | 38 def __init__(self, connection=None, name=None, key_class=GSKey): |
35 super(Bucket, self).__init__(connection, name, key_class) | 39 super(Bucket, self).__init__(connection, name, key_class) |
36 | 40 |
37 def set_acl(self, acl_or_str, key_name='', headers=None, version_id=None): | 41 def set_acl(self, acl_or_str, key_name='', headers=None, version_id=None): |
| 42 """sets or changes a bucket's acl. We include a version_id argument |
| 43 to support a polymorphic interface for callers, however, |
| 44 version_id is not relevant for Google Cloud Storage buckets |
| 45 and is therefore ignored here.""" |
38 if isinstance(acl_or_str, Policy): | 46 if isinstance(acl_or_str, Policy): |
39 raise InvalidAclError('Attempt to set S3 Policy on GS ACL') | 47 raise InvalidAclError('Attempt to set S3 Policy on GS ACL') |
40 elif isinstance(acl_or_str, ACL): | 48 elif isinstance(acl_or_str, ACL): |
41 self.set_xml_acl(acl_or_str.to_xml(), key_name, headers=headers) | 49 self.set_xml_acl(acl_or_str.to_xml(), key_name, headers=headers) |
42 else: | 50 else: |
43 self.set_canned_acl(acl_or_str, key_name, headers=headers) | 51 self.set_canned_acl(acl_or_str, key_name, headers=headers) |
44 | 52 |
45 def get_acl(self, key_name='', headers=None, version_id=None): | 53 def set_def_acl(self, acl_or_str, key_name='', headers=None): |
| 54 """sets or changes a bucket's default object acl""" |
| 55 if isinstance(acl_or_str, Policy): |
| 56 raise InvalidAclError('Attempt to set S3 Policy on GS ACL') |
| 57 elif isinstance(acl_or_str, ACL): |
| 58 self.set_def_xml_acl(acl_or_str.to_xml(), key_name, headers=headers) |
| 59 else: |
| 60 self.set_def_canned_acl(acl_or_str, key_name, headers=headers) |
| 61 |
| 62 def get_acl_helper(self, key_name, headers, query_args): |
| 63 """provides common functionality for get_acl() and get_def_acl()""" |
46 response = self.connection.make_request('GET', self.name, key_name, | 64 response = self.connection.make_request('GET', self.name, key_name, |
47 query_args='acl', headers=headers) | 65 query_args=query_args, |
| 66 headers=headers) |
48 body = response.read() | 67 body = response.read() |
49 if response.status == 200: | 68 if response.status == 200: |
50 acl = ACL(self) | 69 acl = ACL(self) |
51 h = handler.XmlHandler(acl, self) | 70 h = handler.XmlHandler(acl, self) |
52 xml.sax.parseString(body, h) | 71 xml.sax.parseString(body, h) |
53 return acl | 72 return acl |
54 else: | 73 else: |
55 raise self.connection.provider.storage_response_error( | 74 raise self.connection.provider.storage_response_error( |
56 response.status, response.reason, body) | 75 response.status, response.reason, body) |
57 | 76 |
58 def set_canned_acl(self, acl_str, key_name='', headers=None, | 77 def get_acl(self, key_name='', headers=None, version_id=None): |
59 version_id=None): | 78 """returns a bucket's acl. We include a version_id argument |
| 79 to support a polymorphic interface for callers, however, |
| 80 version_id is not relevant for Google Cloud Storage buckets |
| 81 and is therefore ignored here.""" |
| 82 return self.get_acl_helper(key_name, headers, STANDARD_ACL) |
| 83 |
| 84 def get_def_acl(self, key_name='', headers=None): |
| 85 """returns a bucket's default object acl""" |
| 86 return self.get_acl_helper(key_name, headers, DEF_OBJ_ACL) |
| 87 |
| 88 def set_canned_acl_helper(self, acl_str, key_name, headers, query_args): |
| 89 """provides common functionality for set_canned_acl() and |
| 90 set_def_canned_acl()""" |
60 assert acl_str in CannedACLStrings | 91 assert acl_str in CannedACLStrings |
61 | 92 |
62 if headers: | 93 if headers: |
63 headers[self.connection.provider.acl_header] = acl_str | 94 headers[self.connection.provider.acl_header] = acl_str |
64 else: | 95 else: |
65 headers={self.connection.provider.acl_header: acl_str} | 96 headers={self.connection.provider.acl_header: acl_str} |
66 | 97 |
67 query_args='acl' | |
68 if version_id: | |
69 query_args += '&versionId=%s' % version_id | |
70 response = self.connection.make_request('PUT', self.name, key_name, | 98 response = self.connection.make_request('PUT', self.name, key_name, |
71 headers=headers, query_args=query_args) | 99 headers=headers, query_args=query_args) |
72 body = response.read() | 100 body = response.read() |
73 if response.status != 200: | 101 if response.status != 200: |
74 raise self.connection.provider.storage_response_error( | 102 raise self.connection.provider.storage_response_error( |
75 response.status, response.reason, body) | 103 response.status, response.reason, body) |
76 | 104 |
| 105 def set_canned_acl(self, acl_str, key_name='', headers=None, |
| 106 version_id=None): |
| 107 """sets or changes a bucket's acl to a predefined (canned) value. |
| 108 We include a version_id argument to support a polymorphic |
| 109 interface for callers, however, version_id is not relevant for |
| 110 Google Cloud Storage buckets and is therefore ignored here.""" |
| 111 return self.set_canned_acl_helper(acl_str, key_name, headers, |
| 112 STANDARD_ACL) |
| 113 |
| 114 def set_def_canned_acl(self, acl_str, key_name='', headers=None): |
| 115 """sets or changes a bucket's default object acl to a predefined |
| 116 (canned) value""" |
| 117 return self.set_canned_acl_helper(acl_str, key_name, headers, |
| 118 query_args=DEF_OBJ_ACL) |
| 119 |
| 120 def set_def_xml_acl(self, acl_str, key_name='', headers=None): |
| 121 """sets or changes a bucket's default object""" |
| 122 return self.set_xml_acl(acl_str, key_name, headers, |
| 123 query_args=DEF_OBJ_ACL) |
| 124 |
77 # Method with same signature as boto.s3.bucket.Bucket.add_email_grant(), | 125 # Method with same signature as boto.s3.bucket.Bucket.add_email_grant(), |
78 # to allow polymorphic treatment at application layer. | 126 # to allow polymorphic treatment at application layer. |
79 def add_email_grant(self, permission, email_address, | 127 def add_email_grant(self, permission, email_address, |
80 recursive=False, headers=None): | 128 recursive=False, headers=None): |
81 """ | 129 """ |
82 Convenience method that provides a quick way to add an email grant | 130 Convenience method that provides a quick way to add an email grant |
83 to a bucket. This method retrieves the current ACL, creates a new | 131 to a bucket. This method retrieves the current ACL, creates a new |
84 grant based on the parameters passed in, adds that grant to the ACL | 132 grant based on the parameters passed in, adds that grant to the ACL |
85 and then PUT's the new ACL back to GS. | 133 and then PUT's the new ACL back to GS. |
86 | 134 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 xml_str = (xml_str + '<LogBucket>%s</LogBucket>' % target_bucket) | 251 xml_str = (xml_str + '<LogBucket>%s</LogBucket>' % target_bucket) |
204 if target_prefix: | 252 if target_prefix: |
205 xml_str = (xml_str + | 253 xml_str = (xml_str + |
206 '<LogObjectPrefix>%s</LogObjectPrefix>' % target_prefix) | 254 '<LogObjectPrefix>%s</LogObjectPrefix>' % target_prefix) |
207 if canned_acl: | 255 if canned_acl: |
208 xml_str = (xml_str + | 256 xml_str = (xml_str + |
209 '<PredefinedAcl>%s</PredefinedAcl>' % canned_acl) | 257 '<PredefinedAcl>%s</PredefinedAcl>' % canned_acl) |
210 xml_str = xml_str + '</Logging>' | 258 xml_str = xml_str + '</Logging>' |
211 | 259 |
212 self.set_subresource('logging', xml_str, headers=headers) | 260 self.set_subresource('logging', xml_str, headers=headers) |
OLD | NEW |