Skip to content

Commit 068b9fa

Browse files
committed
STORAGE: Removing project from Connection.
Only needed for create_bucket() and get_all_buckets(). Fixes #726.
1 parent d668044 commit 068b9fa

File tree

13 files changed

+119
-221
lines changed

13 files changed

+119
-221
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The first step in accessing Cloud Storage is to create a connection to the
3838
service::
3939

4040
>>> from gcloud import storage
41-
>>> connection = storage.get_connection(project_name)
41+
>>> connection = storage.get_connection()
4242

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

5757
Let's create a bucket:
5858

59-
>>> bucket = storage.create_bucket('test', connection=connection)
59+
>>> bucket = storage.create_bucket('test', project_name, connection=connection)
6060
Traceback (most recent call last):
6161
File "<stdin>", line 1, in <module>
6262
File "gcloud/storage/connection.py", line 340, in create_bucket

gcloud/storage/__init__.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,13 @@ def set_default_project(project=None):
107107
_implicit_environ._DEFAULTS.project = project
108108

109109

110-
def set_default_connection(project=None, connection=None):
110+
def set_default_connection(connection=None):
111111
"""Set default connection either explicitly or implicitly as fall-back.
112112
113-
:type project: string
114-
:param project: Optional. The name of the project to connect to.
115-
116113
:type connection: :class:`gcloud.storage.connection.Connection`
117114
:param connection: A connection provided to be the default.
118115
"""
119-
connection = connection or get_connection(project)
116+
connection = connection or get_connection()
120117
_implicit_environ._DEFAULTS.connection = connection
121118

122119

@@ -134,35 +131,27 @@ def set_defaults(bucket=None, project=None, connection=None):
134131
:type connection: :class:`gcloud.storage.connection.Connection`
135132
:param connection: Optional. A connection provided to be the default.
136133
"""
137-
# NOTE: `set_default_project` is called before `set_default_connection`
138-
# since `set_default_connection` falls back to implicit project.
139134
set_default_project(project=project)
140-
set_default_connection(project=project, connection=connection)
135+
set_default_connection(connection=connection)
141136
# NOTE: `set_default_bucket` is called after `set_default_connection`
142137
# since `set_default_bucket` falls back to implicit connection.
143138
set_default_bucket(bucket=bucket)
144139

145140

146-
def get_connection(project=None):
141+
def get_connection():
147142
"""Shortcut method to establish a connection to Cloud Storage.
148143
149144
Use this if you are going to access several buckets with the same
150145
set of credentials:
151146
152147
>>> from gcloud import storage
153-
>>> connection = storage.get_connection(project)
148+
>>> connection = storage.get_connection()
154149
>>> bucket1 = storage.get_bucket('bucket1', connection=connection)
155150
>>> bucket2 = storage.get_bucket('bucket2', connection=connection)
156151
157-
:type project: string or ``NoneType``
158-
:param project: Optional. The name of the project to connect to. If not
159-
included, falls back to default project.
160-
161152
:rtype: :class:`gcloud.storage.connection.Connection`
162153
:returns: A connection defined with the proper credentials.
163154
"""
164-
if project is None:
165-
project = get_default_project()
166155
implicit_credentials = credentials.get_credentials()
167156
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
168-
return Connection(project=project, credentials=scoped_credentials)
157+
return Connection(credentials=scoped_credentials)

gcloud/storage/acl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
:func:`gcloud.storage.bucket.Bucket.acl`::
2020
2121
>>> from gcloud import storage
22-
>>> connection = storage.get_connection(project)
22+
>>> connection = storage.get_connection()
2323
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
2424
>>> acl = bucket.acl
2525

gcloud/storage/api.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from gcloud.exceptions import NotFound
2222
from gcloud.storage._implicit_environ import get_default_connection
23+
from gcloud.storage._implicit_environ import get_default_project
2324
from gcloud.storage.bucket import Bucket
2425
from gcloud.storage.iterator import Iterator
2526

@@ -59,7 +60,7 @@ def lookup_bucket(bucket_name, connection=None):
5960
return None
6061

6162

62-
def get_all_buckets(connection=None):
63+
def get_all_buckets(project=None, connection=None):
6364
"""Get all buckets in the project.
6465
6566
This will not populate the list of blobs available in each
@@ -71,6 +72,10 @@ def get_all_buckets(connection=None):
7172
7273
This implements "storage.buckets.list".
7374
75+
:type project: string
76+
:param project: Optional. The project to use when listing all buckets.
77+
If not provided, falls back to default.
78+
7479
:type connection: :class:`gcloud.storage.connection.Connection` or
7580
``NoneType``
7681
:param connection: Optional. The connection to use when sending requests.
@@ -81,7 +86,11 @@ def get_all_buckets(connection=None):
8186
"""
8287
if connection is None:
8388
connection = get_default_connection()
84-
return iter(_BucketIterator(connection=connection))
89+
if project is None:
90+
project = get_default_project()
91+
extra_params = {'project': project}
92+
return iter(_BucketIterator(connection=connection,
93+
extra_params=extra_params))
8594

8695

8796
def get_bucket(bucket_name, connection=None):
@@ -121,7 +130,7 @@ def get_bucket(bucket_name, connection=None):
121130
return Bucket(properties=response, connection=connection)
122131

123132

124-
def create_bucket(bucket_name, connection=None):
133+
def create_bucket(bucket_name, project=None, connection=None):
125134
"""Create a new bucket.
126135
127136
For example::
@@ -134,6 +143,10 @@ def create_bucket(bucket_name, connection=None):
134143
135144
This implements "storage.buckets.insert".
136145
146+
:type project: string
147+
:param project: Optional. The project to use when creating bucket.
148+
If not provided, falls back to default.
149+
137150
:type bucket_name: string
138151
:param bucket_name: The bucket name to create.
139152
@@ -149,8 +162,12 @@ def create_bucket(bucket_name, connection=None):
149162
"""
150163
if connection is None:
151164
connection = get_default_connection()
165+
if project is None:
166+
project = get_default_project()
152167

168+
query_params = {'project': project}
153169
response = connection.api_request(method='POST', path='/b',
170+
query_params=query_params,
154171
data={'name': bucket_name})
155172
return Bucket(properties=response, connection=connection)
156173

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

169-
def __init__(self, connection):
170-
super(_BucketIterator, self).__init__(connection=connection, path='/b')
186+
def __init__(self, connection, extra_params=None):
187+
super(_BucketIterator, self).__init__(connection=connection, path='/b',
188+
extra_params=extra_params)
171189

172190
def get_items_from_response(self, response):
173191
"""Factory method which yields :class:`.Bucket` items from a response.

gcloud/storage/batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, connection=None):
8383
if connection is None:
8484
connection = _implicit_environ.get_default_connection()
8585

86-
super(Batch, self).__init__(project=connection.project)
86+
super(Batch, self).__init__()
8787
self._connection = connection
8888
self._requests = []
8989
self._responses = []

gcloud/storage/bucket.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def get_blob(self, blob):
169169
This will return None if the blob doesn't exist::
170170
171171
>>> from gcloud import storage
172-
>>> connection = storage.get_connection(project)
172+
>>> connection = storage.get_connection()
173173
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
174174
>>> print bucket.get_blob('/path/to/blob.txt')
175175
<Blob: my-bucket, /path/to/blob.txt>
@@ -313,7 +313,7 @@ def delete_blob(self, blob):
313313
314314
>>> from gcloud.exceptions import NotFound
315315
>>> from gcloud import storage
316-
>>> connection = storage.get_connection(project)
316+
>>> connection = storage.get_connection()
317317
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
318318
>>> print bucket.get_all_blobs()
319319
[<Blob: my-bucket, my-file.txt>]
@@ -395,7 +395,7 @@ def upload_file(self, filename, blob=None):
395395
For example::
396396
397397
>>> from gcloud import storage
398-
>>> connection = storage.get_connection(project)
398+
>>> connection = storage.get_connection()
399399
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
400400
>>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
401401
>>> print bucket.get_all_blobs()
@@ -406,7 +406,7 @@ def upload_file(self, filename, blob=None):
406406
path)::
407407
408408
>>> from gcloud import storage
409-
>>> connection = storage.get_connection(project)
409+
>>> connection = storage.get_connection()
410410
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
411411
>>> bucket.upload_file('~/my-file.txt')
412412
>>> print bucket.get_all_blobs()
@@ -438,7 +438,7 @@ def upload_file_object(self, file_obj, blob=None):
438438
For example::
439439
440440
>>> from gcloud import storage
441-
>>> connection = storage.get_connection(project)
441+
>>> connection = storage.get_connection()
442442
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
443443
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
444444
>>> print bucket.get_all_blobs()
@@ -449,7 +449,7 @@ def upload_file_object(self, file_obj, blob=None):
449449
path)::
450450
451451
>>> from gcloud import storage
452-
>>> connection = storage.get_connection(project)
452+
>>> connection = storage.get_connection()
453453
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
454454
>>> bucket.upload_file(open('~/my-file.txt'))
455455
>>> print bucket.get_all_blobs()
@@ -704,7 +704,7 @@ def configure_website(self, main_page_suffix=None, not_found_page=None):
704704
of an index page and a page to use when a blob isn't found::
705705
706706
>>> from gcloud import storage
707-
>>> connection = storage.get_connection(project)
707+
>>> connection = storage.get_connection()
708708
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
709709
>>> bucket.configure_website('index.html', '404.html')
710710

gcloud/storage/connection.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class Connection(base_connection.Connection):
3030
:mod:`gcloud.storage.api` and
3131
:class:`gcloud.storage.bucket.Bucket` and
3232
:class:`gcloud.storage.blob.Blob`).
33-
34-
See :class:`gcloud.connection.Connection` for a full list of
35-
parameters. This subclass differs only in needing a project
36-
name (which you specify when creating a project in the Cloud
37-
Console).
3833
"""
3934

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

49-
def __init__(self, project, *args, **kwargs):
50-
""":type project: string
51-
52-
:param project: The project name to connect to.
53-
"""
54-
super(Connection, self).__init__(*args, **kwargs)
55-
self.project = project
56-
57-
def build_api_url(self, path, query_params=None, api_base_url=None,
44+
@classmethod
45+
def build_api_url(cls, path, query_params=None, api_base_url=None,
5846
api_version=None, upload=False):
5947
"""Construct an API url given a few components, some optional.
6048
@@ -82,18 +70,18 @@ def build_api_url(self, path, query_params=None, api_base_url=None,
8270
:rtype: string
8371
:returns: The URL assembled from the pieces provided.
8472
"""
85-
api_base_url = api_base_url or self.API_BASE_URL
73+
api_base_url = api_base_url or cls.API_BASE_URL
8674
if upload:
8775
api_base_url += '/upload'
8876

89-
url = self.API_URL_TEMPLATE.format(
90-
api_base_url=(api_base_url or self.API_BASE_URL),
91-
api_version=(api_version or self.API_VERSION),
77+
url = cls.API_URL_TEMPLATE.format(
78+
api_base_url=(api_base_url or cls.API_BASE_URL),
79+
api_version=(api_version or cls.API_VERSION),
9280
path=path)
9381

9482
query_params = query_params or {}
95-
query_params.update({'project': self.project})
96-
url += '?' + urlencode(query_params)
83+
if query_params:
84+
url += '?' + urlencode(query_params)
9785

9886
return url
9987

gcloud/storage/demo/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
import os
1616
from gcloud import storage
1717

18-
__all__ = ['get_connection', 'PROJECT_ID']
18+
__all__ = ['create_bucket', 'get_all_buckets', 'PROJECT_ID']
1919

2020
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
2121

2222

23-
def get_connection():
24-
return storage.get_connection(PROJECT_ID)
23+
def get_all_buckets(connection):
24+
return list(storage.get_all_buckets(PROJECT_ID, connection))
25+
26+
27+
def create_bucket(bucket_name, connection):
28+
return storage.create_bucket(bucket_name, PROJECT_ID,
29+
connection=connection)

gcloud/storage/demo/demo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
from gcloud import storage
1010
from gcloud.storage import demo
1111

12-
connection = demo.get_connection()
12+
connection = storage.get_connection()
1313

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

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

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

2626
# How about we create a new blob inside this bucket.
2727
blob = bucket.new_blob("my-new-file.txt")

0 commit comments

Comments
 (0)