Skip to content

Commit

Permalink
STORAGE: Removing project from Connection.
Browse files Browse the repository at this point in the history
Only needed for create_bucket() and get_all_buckets().

Fixes #726.
  • Loading branch information
dhermes committed Mar 17, 2015
1 parent c490b68 commit b91b2af
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 221 deletions.
4 changes: 2 additions & 2 deletions docs/_components/storage-getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The first step in accessing Cloud Storage is to create a connection to the
service::

>>> from gcloud import storage
>>> connection = storage.get_connection(project_name)
>>> connection = storage.get_connection()

We're going to use this :class:`connection
<gcloud.storage.connection.Connection>` object for the rest of this guide.
Expand All @@ -56,7 +56,7 @@ bucket.

Let's create a bucket:

>>> bucket = storage.create_bucket('test', connection=connection)
>>> bucket = storage.create_bucket('test', project_name, connection=connection)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gcloud/storage/connection.py", line 340, in create_bucket
Expand Down
23 changes: 6 additions & 17 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,13 @@ def set_default_project(project=None):
_implicit_environ._DEFAULTS.project = project


def set_default_connection(project=None, connection=None):
def set_default_connection(connection=None):
"""Set default connection either explicitly or implicitly as fall-back.
:type project: string
:param project: Optional. The name of the project to connect to.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: A connection provided to be the default.
"""
connection = connection or get_connection(project)
connection = connection or get_connection()
_implicit_environ._DEFAULTS.connection = connection


Expand All @@ -134,35 +131,27 @@ def set_defaults(bucket=None, project=None, connection=None):
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional. A connection provided to be the default.
"""
# NOTE: `set_default_project` is called before `set_default_connection`
# since `set_default_connection` falls back to implicit project.
set_default_project(project=project)
set_default_connection(project=project, connection=connection)
set_default_connection(connection=connection)
# NOTE: `set_default_bucket` is called after `set_default_connection`
# since `set_default_bucket` falls back to implicit connection.
set_default_bucket(bucket=bucket)


def get_connection(project=None):
def get_connection():
"""Shortcut method to establish a connection to Cloud Storage.
Use this if you are going to access several buckets with the same
set of credentials:
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket1 = storage.get_bucket('bucket1', connection=connection)
>>> bucket2 = storage.get_bucket('bucket2', connection=connection)
:type project: string or ``NoneType``
:param project: Optional. The name of the project to connect to. If not
included, falls back to default project.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: A connection defined with the proper credentials.
"""
if project is None:
project = get_default_project()
implicit_credentials = credentials.get_credentials()
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
return Connection(project=project, credentials=scoped_credentials)
return Connection(credentials=scoped_credentials)
2 changes: 1 addition & 1 deletion gcloud/storage/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:func:`gcloud.storage.bucket.Bucket.acl`::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
>>> acl = bucket.acl
Expand Down
28 changes: 23 additions & 5 deletions gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from gcloud.exceptions import NotFound
from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage._implicit_environ import get_default_project
from gcloud.storage.bucket import Bucket
from gcloud.storage.iterator import Iterator

Expand Down Expand Up @@ -59,7 +60,7 @@ def lookup_bucket(bucket_name, connection=None):
return None


def get_all_buckets(connection=None):
def get_all_buckets(project=None, connection=None):
"""Get all buckets in the project.
This will not populate the list of blobs available in each
Expand All @@ -71,6 +72,10 @@ def get_all_buckets(connection=None):
This implements "storage.buckets.list".
:type project: string
:param project: Optional. The project to use when listing all buckets.
If not provided, falls back to default.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending requests.
Expand All @@ -81,7 +86,11 @@ def get_all_buckets(connection=None):
"""
if connection is None:
connection = get_default_connection()
return iter(_BucketIterator(connection=connection))
if project is None:
project = get_default_project()
extra_params = {'project': project}
return iter(_BucketIterator(connection=connection,
extra_params=extra_params))


def get_bucket(bucket_name, connection=None):
Expand Down Expand Up @@ -121,7 +130,7 @@ def get_bucket(bucket_name, connection=None):
return Bucket(properties=response, connection=connection)


def create_bucket(bucket_name, connection=None):
def create_bucket(bucket_name, project=None, connection=None):
"""Create a new bucket.
For example::
Expand All @@ -134,6 +143,10 @@ def create_bucket(bucket_name, connection=None):
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.
:type bucket_name: string
:param bucket_name: The bucket name to create.
Expand All @@ -149,8 +162,12 @@ def create_bucket(bucket_name, connection=None):
"""
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})
return Bucket(properties=response, connection=connection)

Expand All @@ -166,8 +183,9 @@ class _BucketIterator(Iterator):
:param connection: The connection to use for querying the list of buckets.
"""

def __init__(self, connection):
super(_BucketIterator, self).__init__(connection=connection, path='/b')
def __init__(self, connection, extra_params=None):
super(_BucketIterator, self).__init__(connection=connection, path='/b',
extra_params=extra_params)

def get_items_from_response(self, response):
"""Factory method which yields :class:`.Bucket` items from a response.
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, connection=None):
if connection is None:
connection = _implicit_environ.get_default_connection()

super(Batch, self).__init__(project=connection.project)
super(Batch, self).__init__()
self._connection = connection
self._requests = []
self._responses = []
Expand Down
14 changes: 7 additions & 7 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_blob(self, blob_name):
This will return None if the blob doesn't exist::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> print bucket.get_blob('/path/to/blob.txt')
<Blob: my-bucket, /path/to/blob.txt>
Expand Down Expand Up @@ -290,7 +290,7 @@ def delete_blob(self, blob_name):
>>> from gcloud.exceptions import NotFound
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> print bucket.get_all_blobs()
[<Blob: my-bucket, my-file.txt>]
Expand Down Expand Up @@ -371,7 +371,7 @@ def upload_file(self, filename, blob_name=None):
For example::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
>>> print bucket.get_all_blobs()
Expand All @@ -381,7 +381,7 @@ def upload_file(self, filename, blob_name=None):
using the local filename (**not** the complete path)::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> bucket.upload_file('~/my-file.txt')
>>> print bucket.get_all_blobs()
Expand Down Expand Up @@ -413,7 +413,7 @@ def upload_file_object(self, file_obj, blob_name=None):
For example::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
>>> print bucket.get_all_blobs()
Expand All @@ -423,7 +423,7 @@ def upload_file_object(self, file_obj, blob_name=None):
using the local filename (**not** the complete path)::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
>>> bucket.upload_file(open('~/my-file.txt'))
>>> print bucket.get_all_blobs()
Expand Down Expand Up @@ -677,7 +677,7 @@ def configure_website(self, main_page_suffix=None, not_found_page=None):
of an index page and a page to use when a blob isn't found::
>>> from gcloud import storage
>>> connection = storage.get_connection(project)
>>> connection = storage.get_connection()
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
>>> bucket.configure_website('index.html', '404.html')
Expand Down
28 changes: 8 additions & 20 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class Connection(base_connection.Connection):
:mod:`gcloud.storage.api` and
:class:`gcloud.storage.bucket.Bucket` and
:class:`gcloud.storage.blob.Blob`).
See :class:`gcloud.connection.Connection` for a full list of
parameters. This subclass differs only in needing a project
name (which you specify when creating a project in the Cloud
Console).
"""

API_BASE_URL = base_connection.API_BASE_URL
Expand All @@ -46,15 +41,8 @@ class Connection(base_connection.Connection):
API_URL_TEMPLATE = '{api_base_url}/storage/{api_version}{path}'
"""A template for the URL of a particular API call."""

def __init__(self, project, *args, **kwargs):
""":type project: string
:param project: The project name to connect to.
"""
super(Connection, self).__init__(*args, **kwargs)
self.project = project

def build_api_url(self, path, query_params=None, api_base_url=None,
@classmethod
def build_api_url(cls, path, query_params=None, api_base_url=None,
api_version=None, upload=False):
"""Construct an API url given a few components, some optional.
Expand Down Expand Up @@ -82,18 +70,18 @@ def build_api_url(self, path, query_params=None, api_base_url=None,
:rtype: string
:returns: The URL assembled from the pieces provided.
"""
api_base_url = api_base_url or self.API_BASE_URL
api_base_url = api_base_url or cls.API_BASE_URL
if upload:
api_base_url += '/upload'

url = self.API_URL_TEMPLATE.format(
api_base_url=(api_base_url or self.API_BASE_URL),
api_version=(api_version or self.API_VERSION),
url = cls.API_URL_TEMPLATE.format(
api_base_url=(api_base_url or cls.API_BASE_URL),
api_version=(api_version or cls.API_VERSION),
path=path)

query_params = query_params or {}
query_params.update({'project': self.project})
url += '?' + urlencode(query_params)
if query_params:
url += '?' + urlencode(query_params)

return url

Expand Down
11 changes: 8 additions & 3 deletions gcloud/storage/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
import os
from gcloud import storage

__all__ = ['get_connection', 'PROJECT_ID']
__all__ = ['create_bucket', 'get_all_buckets', 'PROJECT_ID']

PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')


def get_connection():
return storage.get_connection(PROJECT_ID)
def get_all_buckets(connection):
return list(storage.get_all_buckets(PROJECT_ID, connection))


def create_bucket(bucket_name, connection):
return storage.create_bucket(bucket_name, PROJECT_ID,
connection=connection)
8 changes: 4 additions & 4 deletions gcloud/storage/demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
from gcloud import storage
from gcloud.storage import demo

connection = demo.get_connection()
connection = storage.get_connection()

# OK, now let's look at all of the buckets...
print(list(storage.get_all_buckets(connection))) # This might take a second...
print(demo.get_all_buckets(connection)) # This might take a second...

# Now let's create a new bucket...
bucket_name = ("bucket-%s" % time.time()).replace(".", "") # Get rid of dots.
print(bucket_name)
bucket = storage.create_bucket(bucket_name, connection=connection)
bucket = demo.create_bucket(bucket_name, connection)
print(bucket)

# Let's look at all of the buckets again...
print(list(storage.get_all_buckets(connection)))
print(demo.get_all_buckets(connection))

# How about we create a new blob inside this bucket.
blob = storage.Blob("my-new-file.txt", bucket=bucket)
Expand Down
Loading

0 comments on commit b91b2af

Please sign in to comment.