/storefront/boto/s3/bucket.py

http://github.com/linkedin/indextank-service · Python · 495 lines · 405 code · 24 blank · 66 comment · 16 complexity · a1a13b0bc8216d5c064336f5b6616027 MD5 · raw file

  1. # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  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
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. import boto
  22. from boto import handler
  23. from boto.resultset import ResultSet
  24. from boto.s3.acl import Policy, CannedACLStrings, ACL, Grant
  25. from boto.s3.user import User
  26. from boto.s3.key import Key
  27. from boto.s3.prefix import Prefix
  28. from boto.exception import S3ResponseError, S3PermissionsError, S3CopyError
  29. from boto.s3.bucketlistresultset import BucketListResultSet
  30. import boto.utils
  31. import xml.sax
  32. import urllib
  33. S3Permissions = ['READ', 'WRITE', 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL']
  34. class Bucket:
  35. BucketLoggingBody = """<?xml version="1.0" encoding="UTF-8"?>
  36. <BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  37. <LoggingEnabled>
  38. <TargetBucket>%s</TargetBucket>
  39. <TargetPrefix>%s</TargetPrefix>
  40. </LoggingEnabled>
  41. </BucketLoggingStatus>"""
  42. EmptyBucketLoggingBody = """<?xml version="1.0" encoding="UTF-8"?>
  43. <BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  44. </BucketLoggingStatus>"""
  45. LoggingGroup = 'http://acs.amazonaws.com/groups/s3/LogDelivery'
  46. BucketPaymentBody = """<?xml version="1.0" encoding="UTF-8"?>
  47. <RequestPaymentConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  48. <Payer>%s</Payer>
  49. </RequestPaymentConfiguration>"""
  50. def __init__(self, connection=None, name=None, key_class=Key):
  51. self.name = name
  52. self.connection = connection
  53. self.key_class = key_class
  54. def __repr__(self):
  55. return '<Bucket: %s>' % self.name
  56. def __iter__(self):
  57. return iter(BucketListResultSet(self))
  58. def __contains__(self, key_name):
  59. return not (self.get_key(key_name) is None)
  60. def startElement(self, name, attrs, connection):
  61. return None
  62. def endElement(self, name, value, connection):
  63. if name == 'Name':
  64. self.name = value
  65. elif name == 'CreationDate':
  66. self.creation_date = value
  67. else:
  68. setattr(self, name, value)
  69. def set_key_class(self, key_class):
  70. """
  71. Set the Key class associated with this bucket. By default, this
  72. would be the boto.s3.key.Key class but if you want to subclass that
  73. for some reason this allows you to associate your new class with a
  74. bucket so that when you call bucket.new_key() or when you get a listing
  75. of keys in the bucket you will get an instances of your key class
  76. rather than the default.
  77. :type key_class: class
  78. :param key_class: A subclass of Key that can be more specific
  79. """
  80. self.key_class = key_class
  81. def lookup(self, key_name, headers=None):
  82. """
  83. Deprecated: Please use get_key method.
  84. :type key_name: string
  85. :param key_name: The name of the key to retrieve
  86. :rtype: :class:`boto.s3.key.Key`
  87. :returns: A Key object from this bucket.
  88. """
  89. return self.get_key(key_name, headers=headers)
  90. def get_key(self, key_name, headers=None):
  91. """
  92. Check to see if a particular key exists within the bucket. This
  93. method uses a HEAD request to check for the existance of the key.
  94. Returns: An instance of a Key object or None
  95. :type key_name: string
  96. :param key_name: The name of the key to retrieve
  97. :rtype: :class:`boto.s3.key.Key`
  98. :returns: A Key object from this bucket.
  99. """
  100. response = self.connection.make_request('HEAD', self.name, key_name, headers=headers)
  101. if response.status == 200:
  102. body = response.read()
  103. k = self.key_class(self)
  104. k.metadata = boto.utils.get_aws_metadata(response.msg)
  105. k.etag = response.getheader('etag')
  106. k.content_type = response.getheader('content-type')
  107. k.content_encoding = response.getheader('content-encoding')
  108. k.last_modified = response.getheader('last-modified')
  109. k.size = int(response.getheader('content-length'))
  110. k.name = key_name
  111. return k
  112. else:
  113. if response.status == 404:
  114. body = response.read()
  115. return None
  116. else:
  117. raise S3ResponseError(response.status, response.reason, '')
  118. def list(self, prefix='', delimiter='', marker='', headers=None):
  119. """
  120. List key objects within a bucket. This returns an instance of an
  121. BucketListResultSet that automatically handles all of the result
  122. paging, etc. from S3. You just need to keep iterating until
  123. there are no more results.
  124. Called with no arguments, this will return an iterator object across
  125. all keys within the bucket.
  126. :type prefix: string
  127. :param prefix: allows you to limit the listing to a particular
  128. prefix. For example, if you call the method with prefix='/foo/'
  129. then the iterator will only cycle through the keys that begin with
  130. the string '/foo/'.
  131. :type delimiter: string
  132. :param delimiter: can be used in conjunction with the prefix
  133. to allow you to organize and browse your keys hierarchically. See:
  134. http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
  135. for more details.
  136. :type marker: string
  137. :param marker: The "marker" of where you are in the result set
  138. :rtype: :class:`boto.s3.bucketlistresultset.BucketListResultSet`
  139. :return: an instance of a BucketListResultSet that handles paging, etc
  140. """
  141. return BucketListResultSet(self, prefix, delimiter, marker, headers)
  142. def get_all_keys(self, headers=None, **params):
  143. """
  144. A lower-level method for listing contents of a bucket. This closely models the actual S3
  145. API and requires you to manually handle the paging of results. For a higher-level method
  146. that handles the details of paging for you, you can use the list method.
  147. :type maxkeys: int
  148. :param maxkeys: The maximum number of keys to retrieve
  149. :type prefix: string
  150. :param prefix: The prefix of the keys you want to retrieve
  151. :type marker: string
  152. :param marker: The "marker" of where you are in the result set
  153. :type delimiter: string
  154. :param delimiter: "If this optional, Unicode string parameter is included with your request, then keys that contain the same string between the prefix and the first occurrence of the delimiter will be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response."
  155. :rtype: ResultSet
  156. :return: The result from S3 listing the keys requested
  157. """
  158. l = []
  159. for k,v in params.items():
  160. if k == 'maxkeys':
  161. k = 'max-keys'
  162. if isinstance(v, unicode):
  163. v = v.encode('utf-8')
  164. if v is not None:
  165. l.append('%s=%s' % (urllib.quote(k), urllib.quote(str(v))))
  166. if len(l):
  167. s = '&'.join(l)
  168. else:
  169. s = None
  170. response = self.connection.make_request('GET', self.name,
  171. headers=headers, query_args=s)
  172. body = response.read()
  173. boto.log.debug(body)
  174. if response.status == 200:
  175. rs = ResultSet([('Contents', self.key_class),
  176. ('CommonPrefixes', Prefix)])
  177. h = handler.XmlHandler(rs, self)
  178. xml.sax.parseString(body, h)
  179. return rs
  180. else:
  181. raise S3ResponseError(response.status, response.reason, body)
  182. def new_key(self, key_name=None):
  183. """
  184. Creates a new key
  185. :type key_name: string
  186. :param key_name: The name of the key to create
  187. :rtype: :class:`boto.s3.key.Key` or subclass
  188. :returns: An instance of the newly created key object
  189. """
  190. return self.key_class(self, key_name)
  191. def generate_url(self, expires_in, method='GET', headers=None, force_http=False):
  192. return self.connection.generate_url(expires_in, method, self.name, headers=headers,
  193. force_http=force_http)
  194. def delete_key(self, key_name, headers=None):
  195. """
  196. Deletes a key from the bucket.
  197. :type key_name: string
  198. :param key_name: The key name to delete
  199. """
  200. response = self.connection.make_request('DELETE', self.name, key_name, headers=headers)
  201. body = response.read()
  202. if response.status != 204:
  203. raise S3ResponseError(response.status, response.reason, body)
  204. def copy_key(self, new_key_name, src_bucket_name, src_key_name, metadata=None):
  205. """
  206. Create a new key in the bucket by copying another existing key.
  207. :type new_key_name: string
  208. :param new_key_name: The name of the new key
  209. :type src_bucket_name: string
  210. :param src_bucket_name: The name of the source bucket
  211. :type src_key_name: string
  212. :param src_key_name: The name of the source key
  213. :type metadata: dict
  214. :param metadata: Metadata to be associated with new key.
  215. If metadata is supplied, it will replace the
  216. metadata of the source key being copied.
  217. If no metadata is supplied, the source key's
  218. metadata will be copied to the new key.
  219. :rtype: :class:`boto.s3.key.Key` or subclass
  220. :returns: An instance of the newly created key object
  221. """
  222. src = '%s/%s' % (src_bucket_name, urllib.quote(src_key_name))
  223. if metadata:
  224. headers = {'x-amz-copy-source' : src,
  225. 'x-amz-metadata-directive' : 'REPLACE'}
  226. headers = boto.utils.merge_meta(headers, metadata)
  227. else:
  228. headers = {'x-amz-copy-source' : src,
  229. 'x-amz-metadata-directive' : 'COPY'}
  230. response = self.connection.make_request('PUT', self.name, new_key_name,
  231. headers=headers)
  232. body = response.read()
  233. if response.status == 200:
  234. key = self.new_key(new_key_name)
  235. h = handler.XmlHandler(key, self)
  236. xml.sax.parseString(body, h)
  237. if hasattr(key, 'Error'):
  238. raise S3CopyError(key.Code, key.Message, body)
  239. return key
  240. else:
  241. raise S3ResponseError(response.status, response.reason, body)
  242. def set_canned_acl(self, acl_str, key_name='', headers=None):
  243. assert acl_str in CannedACLStrings
  244. if headers:
  245. headers['x-amz-acl'] = acl_str
  246. else:
  247. headers={'x-amz-acl': acl_str}
  248. response = self.connection.make_request('PUT', self.name, key_name,
  249. headers=headers, query_args='acl')
  250. body = response.read()
  251. if response.status != 200:
  252. raise S3ResponseError(response.status, response.reason, body)
  253. def get_xml_acl(self, key_name='', headers=None):
  254. response = self.connection.make_request('GET', self.name, key_name,
  255. query_args='acl', headers=headers)
  256. body = response.read()
  257. if response.status != 200:
  258. raise S3ResponseError(response.status, response.reason, body)
  259. return body
  260. def set_xml_acl(self, acl_str, key_name='', headers=None):
  261. response = self.connection.make_request('PUT', self.name, key_name,
  262. data=acl_str, query_args='acl', headers=headers)
  263. body = response.read()
  264. if response.status != 200:
  265. raise S3ResponseError(response.status, response.reason, body)
  266. def set_acl(self, acl_or_str, key_name='', headers=None):
  267. if isinstance(acl_or_str, Policy):
  268. self.set_xml_acl(acl_or_str.to_xml(), key_name, headers=headers)
  269. else:
  270. self.set_canned_acl(acl_or_str, key_name, headers=headers)
  271. def get_acl(self, key_name='', headers=None):
  272. response = self.connection.make_request('GET', self.name, key_name,
  273. query_args='acl', headers=headers)
  274. body = response.read()
  275. if response.status == 200:
  276. policy = Policy(self)
  277. h = handler.XmlHandler(policy, self)
  278. xml.sax.parseString(body, h)
  279. return policy
  280. else:
  281. raise S3ResponseError(response.status, response.reason, body)
  282. def make_public(self, recursive=False, headers=None):
  283. self.set_canned_acl('public-read', headers=headers)
  284. if recursive:
  285. for key in self:
  286. self.set_canned_acl('public-read', key.name, headers=headers)
  287. def add_email_grant(self, permission, email_address, recursive=False, headers=None):
  288. """
  289. Convenience method that provides a quick way to add an email grant to a bucket.
  290. This method retrieves the current ACL, creates a new grant based on the parameters
  291. passed in, adds that grant to the ACL and then PUT's the new ACL back to S3.
  292. :param permission: The permission being granted. Should be one of: (READ, WRITE, READ_ACP, WRITE_ACP, FULL_CONTROL).
  293. See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingAuthAccess.html for more details on permissions.
  294. :type permission: string
  295. :param email_address: The email address associated with the AWS account your are granting
  296. the permission to.
  297. :type email_address: string
  298. :param recursive: A boolean value to controls whether the command will apply the
  299. grant to all keys within the bucket or not. The default value is False.
  300. By passing a True value, the call will iterate through all keys in the
  301. bucket and apply the same grant to each key.
  302. CAUTION: If you have a lot of keys, this could take a long time!
  303. :type recursive: boolean
  304. """
  305. if permission not in S3Permissions:
  306. raise S3PermissionsError('Unknown Permission: %s' % permission)
  307. policy = self.get_acl(headers=headers)
  308. policy.acl.add_email_grant(permission, email_address)
  309. self.set_acl(policy, headers=headers)
  310. if recursive:
  311. for key in self:
  312. key.add_email_grant(permission, email_address, headers=headers)
  313. def add_user_grant(self, permission, user_id, recursive=False, headers=None):
  314. """
  315. Convenience method that provides a quick way to add a canonical user grant to a bucket.
  316. This method retrieves the current ACL, creates a new grant based on the parameters
  317. passed in, adds that grant to the ACL and then PUT's the new ACL back to S3.
  318. :type permission: string
  319. :param permission: The permission being granted. Should be one of:
  320. READ|WRITE|READ_ACP|WRITE_ACP|FULL_CONTROL
  321. See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingAuthAccess.html
  322. for more details on permissions.
  323. :type user_id: string
  324. :param user_id: The canonical user id associated with the AWS account your are granting
  325. the permission to.
  326. :type recursive: bool
  327. :param recursive: A boolean value that controls whether the command will apply the
  328. grant to all keys within the bucket or not. The default value is False.
  329. By passing a True value, the call will iterate through all keys in the
  330. bucket and apply the same grant to each key.
  331. CAUTION: If you have a lot of keys, this could take a long time!
  332. """
  333. if permission not in S3Permissions:
  334. raise S3PermissionsError('Unknown Permission: %s' % permission)
  335. policy = self.get_acl(headers=headers)
  336. policy.acl.add_user_grant(permission, user_id)
  337. self.set_acl(policy, headers=headers)
  338. if recursive:
  339. for key in self:
  340. key.add_user_grant(permission, user_id, headers=headers)
  341. def list_grants(self, headers=None):
  342. policy = self.get_acl(headers=headers)
  343. return policy.acl.grants
  344. def get_location(self):
  345. """
  346. Returns the LocationConstraint for the bucket.
  347. :rtype: str
  348. :return: The LocationConstraint for the bucket or the empty string if
  349. no constraint was specified when bucket was created.
  350. """
  351. response = self.connection.make_request('GET', self.name,
  352. query_args='location')
  353. body = response.read()
  354. if response.status == 200:
  355. rs = ResultSet(self)
  356. h = handler.XmlHandler(rs, self)
  357. xml.sax.parseString(body, h)
  358. return rs.LocationConstraint
  359. else:
  360. raise S3ResponseError(response.status, response.reason, body)
  361. def enable_logging(self, target_bucket, target_prefix='', headers=None):
  362. if isinstance(target_bucket, Bucket):
  363. target_bucket = target_bucket.name
  364. body = self.BucketLoggingBody % (target_bucket, target_prefix)
  365. response = self.connection.make_request('PUT', self.name, data=body,
  366. query_args='logging', headers=headers)
  367. body = response.read()
  368. if response.status == 200:
  369. return True
  370. else:
  371. raise S3ResponseError(response.status, response.reason, body)
  372. def disable_logging(self, headers=None):
  373. body = self.EmptyBucketLoggingBody
  374. response = self.connection.make_request('PUT', self.name, data=body,
  375. query_args='logging', headers=headers)
  376. body = response.read()
  377. if response.status == 200:
  378. return True
  379. else:
  380. raise S3ResponseError(response.status, response.reason, body)
  381. def get_logging_status(self, headers=None):
  382. response = self.connection.make_request('GET', self.name,
  383. query_args='logging', headers=headers)
  384. body = response.read()
  385. if response.status == 200:
  386. return body
  387. else:
  388. raise S3ResponseError(response.status, response.reason, body)
  389. def set_as_logging_target(self, headers=None):
  390. policy = self.get_acl(headers=headers)
  391. g1 = Grant(permission='WRITE', type='Group', uri=self.LoggingGroup)
  392. g2 = Grant(permission='READ_ACP', type='Group', uri=self.LoggingGroup)
  393. policy.acl.add_grant(g1)
  394. policy.acl.add_grant(g2)
  395. self.set_acl(policy, headers=headers)
  396. def disable_logging(self, headers=None):
  397. body = self.EmptyBucketLoggingBody
  398. response = self.connection.make_request('PUT', self.name, data=body,
  399. query_args='logging', headers=headers)
  400. body = response.read()
  401. if response.status == 200:
  402. return True
  403. else:
  404. raise S3ResponseError(response.status, response.reason, body)
  405. def get_request_payment(self, headers=None):
  406. response = self.connection.make_request('GET', self.name,
  407. query_args='requestPayment', headers=headers)
  408. body = response.read()
  409. if response.status == 200:
  410. return body
  411. else:
  412. raise S3ResponseError(response.status, response.reason, body)
  413. def set_request_payment(self, payer='BucketOwner', headers=None):
  414. body = self.BucketPaymentBody % payer
  415. response = self.connection.make_request('PUT', self.name, data=body,
  416. query_args='requestPayment', headers=headers)
  417. body = response.read()
  418. if response.status == 200:
  419. return True
  420. else:
  421. raise S3ResponseError(response.status, response.reason, body)
  422. def delete(self, headers=None):
  423. return self.connection.delete_bucket(self.name, headers=headers)