OLD | NEW |
(Empty) | |
| 1 # Copyright 2010 Google Inc. |
| 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 |
| 22 from boto.gs.bucket import Bucket |
| 23 from boto.s3.connection import S3Connection |
| 24 from boto.s3.connection import SubdomainCallingFormat |
| 25 from boto.s3.connection import check_lowercase_bucketname |
| 26 |
| 27 class Location: |
| 28 DEFAULT = 'US' |
| 29 EU = 'EU' |
| 30 |
| 31 class GSConnection(S3Connection): |
| 32 |
| 33 DefaultHost = 'storage.googleapis.com' |
| 34 QueryString = 'Signature=%s&Expires=%d&AWSAccessKeyId=%s' |
| 35 |
| 36 def __init__(self, gs_access_key_id=None, gs_secret_access_key=None, |
| 37 is_secure=True, port=None, proxy=None, proxy_port=None, |
| 38 proxy_user=None, proxy_pass=None, |
| 39 host=DefaultHost, debug=0, https_connection_factory=None, |
| 40 calling_format=SubdomainCallingFormat(), path='/', |
| 41 suppress_consec_slashes=True): |
| 42 S3Connection.__init__(self, gs_access_key_id, gs_secret_access_key, |
| 43 is_secure, port, proxy, proxy_port, proxy_user, proxy_pass, |
| 44 host, debug, https_connection_factory, calling_format, path, |
| 45 "google", Bucket, |
| 46 suppress_consec_slashes=suppress_consec_slashes) |
| 47 |
| 48 def create_bucket(self, bucket_name, headers=None, |
| 49 location=Location.DEFAULT, policy=None, |
| 50 storage_class='STANDARD'): |
| 51 """ |
| 52 Creates a new bucket. By default it's located in the USA. You can |
| 53 pass Location.EU to create an European bucket. You can also pass |
| 54 a LocationConstraint for where the bucket should be located, and |
| 55 a StorageClass describing how the data should be stored. |
| 56 |
| 57 :type bucket_name: string |
| 58 :param bucket_name: The name of the new bucket. |
| 59 |
| 60 :type headers: dict |
| 61 :param headers: Additional headers to pass along with the request to GCS
. |
| 62 |
| 63 :type location: :class:`boto.gs.connection.Location` |
| 64 :param location: The location of the new bucket. |
| 65 |
| 66 :type policy: :class:`boto.gs.acl.CannedACLStrings` |
| 67 :param policy: A canned ACL policy that will be applied to the new key |
| 68 in GCS. |
| 69 |
| 70 :type storage_class: string |
| 71 :param storage_class: Either 'STANDARD' or 'DURABLE_REDUCED_AVAILABILITY
'. |
| 72 |
| 73 """ |
| 74 check_lowercase_bucketname(bucket_name) |
| 75 |
| 76 if policy: |
| 77 if headers: |
| 78 headers[self.provider.acl_header] = policy |
| 79 else: |
| 80 headers = {self.provider.acl_header : policy} |
| 81 if not location: |
| 82 location = Location.DEFAULT |
| 83 location_elem = ('<LocationConstraint>%s</LocationConstraint>' |
| 84 % location) |
| 85 if storage_class: |
| 86 storage_class_elem = ('<StorageClass>%s</StorageClass>' |
| 87 % storage_class) |
| 88 else: |
| 89 storage_class_elem = '' |
| 90 data = ('<CreateBucketConfiguration>%s%s</CreateBucketConfiguration>' |
| 91 % (location_elem, storage_class_elem)) |
| 92 response = self.make_request('PUT', bucket_name, headers=headers, |
| 93 data=data) |
| 94 body = response.read() |
| 95 if response.status == 409: |
| 96 raise self.provider.storage_create_error( |
| 97 response.status, response.reason, body) |
| 98 if response.status == 200: |
| 99 return self.bucket_class(self, bucket_name) |
| 100 else: |
| 101 raise self.provider.storage_response_error( |
| 102 response.status, response.reason, body) |
| 103 |
OLD | NEW |