1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Implementation of CloudBucket using Google Cloud Storage as the backend."""
9 # boto is located in depot_tools/third_party/
10 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
11 os.pardir, os.pardir, os.pardir, os.pardir,
12 'depot_tools', 'third_party'))
15 from ..common import cloud_bucket
18 class BotoCloudBucket(cloud_bucket.BaseCloudBucket):
19 """Interfaces with GS using the boto library."""
21 def __init__(self, key, secret, bucket_name):
22 """Initializes the bucket with a key, secret, and bucket_name.
25 key: the API key to access GS.
26 secret: the API secret to access GS.
27 bucket_name: the name of the bucket to connect to.
29 uri = boto.storage_uri('', 'gs')
30 conn = uri.connect(key, secret)
31 self.bucket = conn.get_bucket(bucket_name)
33 def _GetKey(self, path):
34 key = boto.gs.key.Key(self.bucket)
39 def UploadFile(self, path, contents, content_type):
40 key = self._GetKey(path)
41 key.set_metadata('Content-Type', content_type)
42 key.set_contents_from_string(contents)
43 # Open permissions for the appengine account to read/write.
44 key.add_email_grant('FULL_CONTROL',
45 'ispy.google.com@appspot.gserviceaccount.com')
48 def DownloadFile(self, path):
49 key = self._GetKey(path)
51 return key.get_contents_as_string()
53 raise cloud_bucket.FileNotFoundError
56 def UpdateFile(self, path, contents):
57 key = self._GetKey(path)
59 key.set_contents_from_string(contents)
61 raise cloud_bucket.FileNotFoundError
64 def RemoveFile(self, path):
65 key = self._GetKey(path)
69 def FileExists(self, path):
70 key = self._GetKey(path)
74 def GetImageURL(self, path):
75 key = self._GetKey(path)
77 # Corrects a bug in boto that incorrectly generates a url
78 # to a resource in Google Cloud Storage.
79 return key.generate_url(3600).replace('AWSAccessKeyId', 'GoogleAccessId')
81 raise cloud_bucket.FileNotFoundError(path)
84 def GetAllPaths(self, prefix):
85 return (key.key for key in self.bucket.get_all_keys(prefix=prefix))