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. 4 5 """Implementation of CloudBucket using Google Cloud Storage as the backend.""" 6 7 import sys 8 sys.path.append(sys.path.join('gsutil', 'third_party', 'boto')) 9 # TODO(chris) check boto into ispy/third_party/ 10 import boto 11 12 from tests.rendering_test_manager import cloud_bucket 13 14 15 class CloudBucketImpl(cloud_bucket.CloudBucket): 16 """Subclass of cloud_bucket.CloudBucket with actual GCS commands.""" 17 18 def __init__(self, key, secret, bucket_name): 19 """Initializes the bucket with a key, secret, and bucket_name. 20 21 Args: 22 key: the API key to access GCS. 23 secret: the API secret to access GCS. 24 bucket_name: the name of the bucket to connect to. 25 26 Returns: 27 Instance of CloudBucketImpl. 28 """ 29 uri = boto.storage_uri('', 'gs') 30 conn = uri.connect(key, secret) 31 self.bucket = conn.get_bucket(bucket_name) 32 33 # override 34 def UploadFile(self, path, contents, content_type): 35 key = boto.gs.key.Key(self.bucket) 36 key.set_metadata('Content-Type', content_type) 37 key.key = path 38 key.set_contents_from_string(contents) 39 40 # override 41 def DownloadFile(self, path): 42 key = boto.gs.key.Key(self.bucket) 43 key.key = path 44 if key.exists(): 45 return key.get_contents_as_string() 46 else: 47 raise cloud_bucket.FileNotFoundError 48 49 # override 50 def RemoveFile(self, path): 51 key = boto.gs.key.Key(self.bucket) 52 key.key = path 53 key.delete() 54 55 # override 56 def FileExists(self, path): 57 key = boto.gs.key.Key(self.bucket) 58 key.key = path 59 return key.exists() 60 61 # override 62 def GetURL(self, path): 63 key = boto.gs.key.Key(self.bucket) 64 key.key = path 65 if key.exists(): 66 return key.generate_url(3600) 67 else: 68 raise cloud_bucket.FileNotFoundError(path) 69 70 # override 71 def GetAllPaths(self, prefix): 72 return (key.key for key in self.bucket.get_all_keys(prefix=prefix)) 73