Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Using a fallback connection in Bucket#759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -46,14 +46,11 @@ def lookup_bucket(bucket_name, connection=None): | ||
| :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| ``NoneType`` | ||
| :param connection: Optional. The connection to use when sending requests. | ||
| If not provided, falls back to default. | ||
| If not provided, Bucket() will fall back to default. | ||
| :rtype: :class:`gcloud.storage.bucket.Bucket` | ||
| :returns: The bucket matching the name provided or None if not found. | ||
| """ | ||
| if connection is None: | ||
| connection = get_default_connection() | ||
| try: | ||
| return get_bucket(bucket_name, connection=connection) | ||
| except NotFound: | ||
| @@ -79,13 +76,12 @@ def get_all_buckets(project=None, connection=None): | ||
| :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| ``NoneType`` | ||
| :param connection: Optional. The connection to use when sending requests. | ||
| If not provided, falls back to default. | ||
| If not provided, _BucketIterator() will fall back to | ||
| default. | ||
| :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects. | ||
| :returns: All buckets belonging to this project. | ||
| """ | ||
| if connection is None: | ||
| connection = get_default_connection() | ||
| if project is None: | ||
| project = get_default_project() | ||
| extra_params = {'project': project} | ||
| @@ -116,15 +112,11 @@ def get_bucket(bucket_name, connection=None): | ||
| :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| ``NoneType`` | ||
| :param connection: Optional. The connection to use when sending requests. | ||
| If not provided, falls back to default. | ||
| If not provided, Bucket() will fall back to default. | ||
| :rtype: :class:`gcloud.storage.bucket.Bucket` | ||
| :returns: The bucket matching the name provided. | ||
| :raises: :class:`gcloud.exceptions.NotFound` | ||
| """ | ||
| if connection is None: | ||
| connection = get_default_connection() | ||
| bucket = Bucket(bucket_name, connection=connection) | ||
| bucket._reload_properties() | ||
| return bucket | ||
| @@ -143,6 +135,9 @@ def create_bucket(bucket_name, project=None, connection=None): | ||
| This implements "storage.buckets.insert". | ||
| If the bucket already exists, will raise | ||
| :class:`gcloud.exceptions.Conflict`. | ||
| :type project: string | ||
| :param project: Optional. The project to use when creating bucket. | ||
| If not provided, falls back to default. | ||
| @@ -153,25 +148,13 @@ def create_bucket(bucket_name, project=None, connection=None): | ||
| :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| ``NoneType`` | ||
| :param connection: Optional. The connection to use when sending requests. | ||
| If not provided, falls back to default. | ||
| If not provided, Bucket() will fall back to default. | ||
| :rtype: :class:`gcloud.storage.bucket.Bucket` | ||
| :returns: The newly created bucket. | ||
| :raises: :class:`gcloud.exceptions.Conflict` if | ||
| there is a confict (bucket already exists, invalid name, etc.) | ||
| """ | ||
| if connection is None: | ||
| connection = get_default_connection() | ||
| if project is None: | ||
| project = get_default_project() | ||
| query_params = {'project': project} | ||
| response = connection.api_request(method='POST', path='/b', | ||
| query_params=query_params, | ||
| data={'name': bucket_name}) | ||
| name = response.get('name') | ||
| bucket = Bucket(name, connection=connection) | ||
| bucket._properties = response | ||
| bucket = Bucket(bucket_name, connection=connection) | ||
| bucket.create(project) | ||
| return bucket | ||
| @@ -187,6 +170,11 @@ class _BucketIterator(Iterator): | ||
| """ | ||
| def __init__(self, connection, extra_params=None): | ||
| # If an implicit connection was intended, we pass along `None` to the | ||
| # Bucket() constructor as well. | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| self._ctor_connection = connection | ||
| if connection is None: | ||
| connection = get_default_connection() | ||
| super(_BucketIterator, self).__init__(connection=connection, path='/b', | ||
| extra_params=extra_params) | ||
| @@ -198,6 +186,6 @@ def get_items_from_response(self, response): | ||
| """ | ||
| for item in response.get('items', []): | ||
| name = item.get('name') | ||
| bucket = Bucket(name, connection=self.connection) | ||
| bucket = Bucket(name, connection=self._ctor_connection) | ||
| bucket._properties = item | ||
| yield bucket | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -36,12 +36,15 @@ | ||
| import os | ||
| import six | ||
| from gcloud._helpers import get_default_project | ||
| from gcloud.exceptions import NotFound | ||
| from gcloud.storage._helpers import _PropertyMixin | ||
| from gcloud.storage._helpers import _scalar_property | ||
| from gcloud.storage import _implicit_environ | ||
| from gcloud.storage.acl import BucketACL | ||
| from gcloud.storage.acl import DefaultObjectACL | ||
| from gcloud.storage.iterator import Iterator | ||
| from gcloud.storage.batch import Batch | ||
| from gcloud.storage.blob import Blob | ||
| @@ -125,6 +128,34 @@ def exists(self): | ||
| except NotFound: | ||
| return False | ||
| def create(self, project=None): | ||
| """Creates current bucket. | ||
| If the bucket already exists, will raise | ||
| :class:`gcloud.exceptions.Conflict`. | ||
| This implements "storage.buckets.insert". | ||
| :type project: string | ||
| :param project: Optional. The project to use when creating bucket. | ||
| If not provided, falls back to default. | ||
| :rtype: :class:`gcloud.storage.bucket.Bucket` | ||
| :returns: The newly created bucket. | ||
| :raises: :class:`EnvironmentError` if the project is not given and | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| can't be inferred. | ||
| """ | ||
| if project is None: | ||
| project = get_default_project() | ||
| if project is None: | ||
| raise EnvironmentError('Project could not be inferred ' | ||
| 'from environment.') | ||
| query_params = {'project': project} | ||
| self._properties = self.connection.api_request( | ||
| method='POST', path='/b', query_params=query_params, | ||
| data={'name': self.name}) | ||
| @property | ||
| def acl(self): | ||
| """Create our ACL on demand.""" | ||
| @@ -146,7 +177,7 @@ def connection(self): | ||
| :rtype: :class:`gcloud.storage.connection.Connection` | ||
| :returns: The connection to use. | ||
| """ | ||
| return self._connection | ||
| return _require_connection(self._connection) | ||
| @staticmethod | ||
| def path_helper(bucket_name): | ||
| @@ -746,3 +777,27 @@ def make_public(self, recursive=False, future=False): | ||
| for blob in self: | ||
| blob.acl.all().grant_read() | ||
| blob.save_acl() | ||
| def _require_connection(connection=None): | ||
| """Infer a connection from the environment, if not passed explicitly. | ||
| :type connection: :class:`gcloud.storage.connection.Connection` | ||
| :param connection: Optional. | ||
| :rtype: :class:`gcloud.storage.connection.Connection` | ||
| :returns: A connection based on the current environment. | ||
| :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and | ||
| cannot be inferred from the environment. | ||
| """ | ||
| # NOTE: We use current Batch directly since it inherits from Connection. | ||
| if connection is None: | ||
| connection = Batch.current() | ||
| if connection is None: | ||
| connection = _implicit_environ.get_default_connection() | ||
| if connection is None: | ||
| raise EnvironmentError('Connection could not be inferred.') | ||
| return connection | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.