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