|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Firebase Cloud Storage module. |
| 16 | +
|
| 17 | +This module contains utilities for accessing Google Cloud Storage buckets associated with |
| 18 | +Firebase apps. This requires installing the google-cloud-storage Python module separately. |
| 19 | +""" |
| 20 | + |
| 21 | +# pylint: disable=import-error,no-name-in-module |
| 22 | +try: |
| 23 | + from google.cloud import storage |
| 24 | +except ImportError: |
| 25 | + raise ImportError('Failed to import the Cloud Storage library for Python. Make sure ' |
| 26 | + 'to install the "google-cloud-storage" module.') |
| 27 | + |
| 28 | +import six |
| 29 | + |
| 30 | +from firebase_admin import utils |
| 31 | + |
| 32 | + |
| 33 | +_STORAGE_ATTRIBUTE = '_storage' |
| 34 | + |
| 35 | +def bucket(name=None, app=None): |
| 36 | + """Returns a handle to a Google Cloud Storage bucket. |
| 37 | +
|
| 38 | + If the name argument is not provided, uses the 'storageBucket' option specified when |
| 39 | + initializing the App. If that is also not available raises an error. This function |
| 40 | + does not make any RPC calls. |
| 41 | +
|
| 42 | + Args: |
| 43 | + name: Name of a cloud storage bucket (optional). |
| 44 | + app: An App instance (optional). |
| 45 | +
|
| 46 | + Returns: |
| 47 | + google.cloud.storage.Bucket: A handle to the specified bucket. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + ValueError: If a bucket name is not specified either via options or method arguments, |
| 51 | + or if the specified bucket name is not a valid string. |
| 52 | + """ |
| 53 | + client = utils.get_app_service(app, _STORAGE_ATTRIBUTE, _StorageClient.from_app) |
| 54 | + return client.bucket(name) |
| 55 | + |
| 56 | + |
| 57 | +class _StorageClient(object): |
| 58 | + """Holds a Google Cloud Storage client instance.""" |
| 59 | + |
| 60 | + def __init__(self, credentials, project, default_bucket): |
| 61 | + self._client = storage.Client(credentials=credentials, project=project) |
| 62 | + self._default_bucket = default_bucket |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def from_app(cls, app): |
| 66 | + credentials = app.credential.get_credential() |
| 67 | + # Specifying project ID is not required, but providing it when available |
| 68 | + # significantly speeds up the initialization of the storage client. |
| 69 | + try: |
| 70 | + project = app.credential.project_id |
| 71 | + except AttributeError: |
| 72 | + project = None |
| 73 | + default_bucket = app.options.get('storageBucket') |
| 74 | + return _StorageClient(credentials, project, default_bucket) |
| 75 | + |
| 76 | + def bucket(self, name=None): |
| 77 | + """Returns a handle to the specified Cloud Storage Bucket.""" |
| 78 | + bucket_name = name if name is not None else self._default_bucket |
| 79 | + if bucket_name is None: |
| 80 | + raise ValueError( |
| 81 | + 'Storage bucket name not specified. Specify the bucket name via the ' |
| 82 | + '"storageBucket" option when initializing the App, or specify the bucket ' |
| 83 | + 'name explicitly when calling the storage.bucket() function.') |
| 84 | + elif not bucket_name or not isinstance(bucket_name, six.string_types): |
| 85 | + raise ValueError( |
| 86 | + 'Invalid storage bucket name: "{0}". Bucket name must be a non-empty ' |
| 87 | + 'string.'.format(bucket_name)) |
| 88 | + return self._client.bucket(bucket_name) |
0 commit comments