- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / functional / ispy / client / boto_bucket.py
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 import os
7 import sys
8
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'))
13 import boto
14
15 from ..common import cloud_bucket
16
17
18 class BotoCloudBucket(cloud_bucket.BaseCloudBucket):
19   """Interfaces with GS using the boto library."""
20
21   def __init__(self, key, secret, bucket_name):
22     """Initializes the bucket with a key, secret, and bucket_name.
23
24     Args:
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.
28     """
29     uri = boto.storage_uri('', 'gs')
30     conn = uri.connect(key, secret)
31     self.bucket = conn.get_bucket(bucket_name)
32
33   def _GetKey(self, path):
34     key = boto.gs.key.Key(self.bucket)
35     key.key = path
36     return key
37
38   # override
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') 
46
47   # override
48   def DownloadFile(self, path):
49     key = self._GetKey(path)
50     if key.exists():
51       return key.get_contents_as_string()
52     else:
53       raise cloud_bucket.FileNotFoundError
54
55   # override
56   def UpdateFile(self, path, contents):
57     key = self._GetKey(path)
58     if key.exists():
59       key.set_contents_from_string(contents)
60     else:
61       raise cloud_bucket.FileNotFoundError
62
63   # override
64   def RemoveFile(self, path):
65     key = self._GetKey(path)
66     key.delete()
67
68   # override
69   def FileExists(self, path):
70     key = self._GetKey(path)
71     return key.exists()
72
73   # override
74   def GetImageURL(self, path):
75     key = self._GetKey(path)
76     if key.exists():
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')
80     else:
81       raise cloud_bucket.FileNotFoundError(path)
82
83   # override
84   def GetAllPaths(self, prefix):
85     return (key.key for key in self.bucket.get_all_keys(prefix=prefix))