| OLD | NEW |
| (Empty) | |
| 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 import os |
| 6 import environment |
| 7 |
| 8 from empty_dir_file_system import EmptyDirFileSystem |
| 9 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF |
| 10 from local_file_system import LocalFileSystem |
| 11 from path_util import IsDirectory |
| 12 |
| 13 class CloudStorageFileSystemProvider(object): |
| 14 '''Provides CloudStorageFileSystem bound to a GCS bucket. |
| 15 ''' |
| 16 def __init__(self, object_store_creator): |
| 17 self._object_store_creator = object_store_creator |
| 18 |
| 19 def Create(self, bucket): |
| 20 '''Creates a CloudStorageFileSystemProvider. |
| 21 |
| 22 |bucket| is the name of GCS bucket, eg devtools-docs. It is expected |
| 23 that this bucket has Read permission for this app in its ACLs. |
| 24 |
| 25 Optional configuration can be set in a local_debug/gcs_debug.conf file: |
| 26 use_local_fs=True|False |
| 27 access_token=<token> |
| 28 remote_bucket_prefix=<prefix> |
| 29 |
| 30 If running in Preview mode or in Development mode with use_local_fs set to |
| 31 True, buckets and files are looked inside the local_debug folder instead |
| 32 of in the real GCS server. Preview server does not support direct GCS |
| 33 access, so it is always forced to use a LocalFileSystem. |
| 34 |
| 35 For real GCS access in the Development mode (dev_appserver.py), |
| 36 access_token and remote_bucket_prefix options can be |
| 37 used to change the way GCS files are accessed. Both are ignored in a real |
| 38 appengine instance. |
| 39 |
| 40 "access_token" is always REQUIRED on dev_appengine, otherwise you will |
| 41 get 404 (auth) errors. You can get one access_token valid for a few minutes |
| 42 by typing: |
| 43 gsutil -d ls 2>&1 | grep "Bearer" | |
| 44 sed "s/.*Bearer \(.*\).r.nUser-Agent.*/access_token=\1/" )" |
| 45 |
| 46 A sample output would be: |
| 47 access_token=ya29.1.AADtN_VW5ibbfLHV5cMIK5ss4bHtVzBXpa4byjd |
| 48 |
| 49 Now add this line to the local_debug/gcs_debug.conf file and restart the |
| 50 appengine development server. |
| 51 |
| 52 Remember that you will need a new access_token every ten minutes or |
| 53 so. If you get 404 errors on log, update it. Access token is not |
| 54 used for a deployed appengine app, only if you use dev_appengine.py. |
| 55 |
| 56 remote_bucket_prefix is useful if you want to test on your own GCS buckets |
| 57 before using the real GCS buckets. |
| 58 |
| 59 ''' |
| 60 if not environment.IsReleaseServer() and not environment.IsDevServer(): |
| 61 bucket_local_path = os.path.join(LOCAL_GCS_DIR, bucket) |
| 62 if IsDirectory(bucket_local_path): |
| 63 return LocalFileSystem(bucket_local_path) |
| 64 else: |
| 65 return EmptyDirFileSystem() |
| 66 |
| 67 debug_access_token = None |
| 68 debug_bucket_prefix = None |
| 69 use_local_fs = False |
| 70 |
| 71 if environment.IsDevServer() and os.path.exists(LOCAL_GCS_DEBUG_CONF): |
| 72 with open(LOCAL_GCS_DEBUG_CONF, "r") as token_file: |
| 73 properties = dict(line.strip().split('=', 1) for line in token_file) |
| 74 use_local_fs = properties.get('use_local_fs', 'False')=='True' |
| 75 debug_access_token = properties.get('access_token', None) |
| 76 debug_bucket_prefix = properties.get('remote_bucket_prefix', None) |
| 77 |
| 78 if environment.IsDevServer() and use_local_fs: |
| 79 return LocalFileSystem(os.path.join(LOCAL_GCS_DIR, bucket)) |
| 80 |
| 81 # gcs_file_system has strong dependencies on runtime appengine APIs, |
| 82 # so we only import it when we are sure we are not on preview.py or tests. |
| 83 from gcs_file_system import CloudStorageFileSystem |
| 84 return CloudStorageFileSystem( |
| 85 bucket, debug_access_token, debug_bucket_prefix) |
| 86 |
| 87 |
| 88 @staticmethod |
| 89 def ForEmpty(): |
| 90 class EmptyImpl(object): |
| 91 def Create(self, bucket): |
| 92 return EmptyDirFileSystem() |
| 93 return EmptyImpl() |
| OLD | NEW |