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."""
11 from ..common import cloud_bucket
14 class GoogleCloudStorageBucket(cloud_bucket.BaseCloudBucket):
15 """Subclass of cloud_bucket.CloudBucket with actual GS commands."""
17 def __init__(self, bucket):
18 """Initializes the bucket.
21 bucket: the name of the bucket to connect to.
23 self.bucket = '/' + bucket
25 def _full_path(self, path):
26 return self.bucket + '/' + path.lstrip('/')
29 def UploadFile(self, path, contents, content_type):
30 gs_file = cloudstorage.open(
31 self._full_path(path), 'w', content_type=content_type)
32 gs_file.write(contents)
36 def DownloadFile(self, path):
38 gs_file = cloudstorage.open(self._full_path(path), 'r')
41 except Exception as e:
42 raise Exception('%s: %s' % (self._full_path(path), str(e)))
46 def UpdateFile(self, path, contents):
47 if not self.FileExists(path):
48 raise cloud_bucket.FileNotFoundError
49 gs_file = cloudstorage.open(self._full_path(path), 'w')
50 gs_file.write(contents)
54 def RemoveFile(self, path):
55 cloudstorage.delete(self._full_path(path))
58 def FileExists(self, path):
60 cloudstorage.stat(self._full_path(path))
61 except cloudstorage.NotFoundError:
66 def GetImageURL(self, path):
67 return '/image?file_path=%s' % path
70 def GetAllPaths(self, prefix):
71 return (f.filename[len(self.bucket) + 1:] for f in
72 cloudstorage.listbucket(self.bucket, prefix=prefix))