Skip to content

Commit 447bba1

Browse files
committed
Merge pull request #721 from dhermes/storage-move-bucket-methods
STORAGE: Moving get_bucket, create_bucket and delete_bucket off of Connection.
2 parents 982dd56 + ef75066 commit 447bba1

File tree

16 files changed

+254
-326
lines changed

16 files changed

+254
-326
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ to Cloud Storage using this Client Library.
9999
.. code:: python
100100
101101
from gcloud import storage
102-
bucket = storage.get_bucket('bucket-id-here', 'project-id')
102+
storage.set_defaults()
103+
bucket = storage.get_bucket('bucket-id-here')
103104
# Then do other things...
104105
blob = bucket.get_blob('/remote/path/to/file.txt')
105106
print blob.download_as_string()

docs/_components/storage-getting-started.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bucket.
5656

5757
Let's create a bucket:
5858

59-
>>> bucket = connection.create_bucket('test')
59+
>>> bucket = storage.create_bucket('test', 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
@@ -151,11 +151,11 @@ in Python::
151151
Accessing a bucket
152152
------------------
153153

154-
If you already have a bucket, use :func:`get_bucket
155-
<gcloud.storage.connection.Connection.get_bucket>` to retrieve the bucket
156-
object::
154+
If you already have a bucket, use
155+
:func:`get_bucket <gcloud.storage.api.get_bucket>` to retrieve the
156+
bucket object::
157157

158-
>>> bucket = connection.get_bucket('my-bucket')
158+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
159159

160160
If you want to get all the blobs in the bucket, you can use
161161
:func:`get_all_blobs <gcloud.storage.bucket.Bucket.get_all_blobs>`::
@@ -171,17 +171,17 @@ bucket itself as an iterator::
171171
Deleting a bucket
172172
-----------------
173173

174-
You can delete a bucket using the :func:`delete_bucket
175-
<gcloud.storage.connection.Connection.delete_bucket>` method::
174+
You can delete a bucket using the
175+
:meth:`delete <gcloud.storage.bucket.Bucket.delete>` method::
176176

177-
>>> connection.delete_bucket('my-bucket')
177+
>>> bucket.delete()
178178

179179
Remember, the bucket you're deleting needs to be empty, otherwise you'll
180-
get an error.
180+
get an error (409 conflict).
181181

182182
If you have a full bucket, you can delete it this way::
183183

184-
>>> bucket = connection.delete_bucket('my-bucket', force=True)
184+
>>> bucket.delete(force=True)
185185

186186
Listing available buckets
187187
-------------------------

docs/_components/storage-quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ you can create buckets and blobs::
5858
>>> from gcloud import storage
5959
>>> storage.get_all_buckets(connection)
6060
[<Bucket: ...>, ...]
61-
>>> bucket = connection.create_bucket('my-new-bucket')
61+
>>> bucket = storage.create_bucket('my-new-bucket', connection=connection)
6262
>>> print bucket
6363
<Bucket: my-new-bucket>
6464
>>> blob = bucket.new_blob('my-test-file.txt')

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Cloud Storage
4747
.. code-block:: python
4848
4949
from gcloud import storage
50-
bucket = storage.get_bucket('<your-bucket-name>', '<your-project-id>')
50+
storage.set_defaults()
51+
bucket = storage.get_bucket('<your-bucket-name>')
5152
blob = bucket.new_blob('my-test-file.txt')
5253
blob = blob.upload_contents_from_string('this is test content!')

gcloud/storage/__init__.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
1717
You'll typically use these to get started with the API:
1818
19-
>>> import gcloud.storage
20-
>>> bucket = gcloud.storage.get_bucket('bucket-id-here', 'project-id')
19+
>>> from gcloud import storage
20+
>>> storage.set_defaults()
21+
>>> bucket = storage.get_bucket('bucket-id-here')
2122
>>> # Then do other things...
2223
>>> blob = bucket.get_blob('/remote/path/to/file.txt')
2324
>>> print blob.download_as_string()
@@ -44,7 +45,9 @@
4445
from gcloud.storage._implicit_environ import get_default_bucket
4546
from gcloud.storage._implicit_environ import get_default_connection
4647
from gcloud.storage._implicit_environ import get_default_project
48+
from gcloud.storage.api import create_bucket
4749
from gcloud.storage.api import get_all_buckets
50+
from gcloud.storage.api import get_bucket
4851
from gcloud.storage.api import lookup_bucket
4952
from gcloud.storage.blob import Blob
5053
from gcloud.storage.bucket import Bucket
@@ -148,8 +151,8 @@ def get_connection(project=None):
148151
149152
>>> from gcloud import storage
150153
>>> connection = storage.get_connection(project)
151-
>>> bucket1 = connection.get_bucket('bucket1')
152-
>>> bucket2 = connection.get_bucket('bucket2')
154+
>>> bucket1 = storage.get_bucket('bucket1', connection=connection)
155+
>>> bucket2 = storage.get_bucket('bucket2', connection=connection)
153156
154157
:type project: string or ``NoneType``
155158
:param project: Optional. The name of the project to connect to. If not
@@ -163,28 +166,3 @@ def get_connection(project=None):
163166
implicit_credentials = credentials.get_credentials()
164167
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
165168
return Connection(project=project, credentials=scoped_credentials)
166-
167-
168-
def get_bucket(bucket_name, project):
169-
"""Shortcut method to establish a connection to a particular bucket.
170-
171-
You'll generally use this as the first call to working with the API:
172-
173-
>>> from gcloud import storage
174-
>>> bucket = storage.get_bucket(project, bucket_name)
175-
>>> # Now you can do things with the bucket.
176-
>>> bucket.exists('/path/to/file.txt')
177-
False
178-
179-
:type bucket_name: string
180-
:param bucket_name: The id of the bucket you want to use.
181-
This is akin to a disk name on a file system.
182-
183-
:type project: string
184-
:param project: The name of the project to connect to.
185-
186-
:rtype: :class:`gcloud.storage.bucket.Bucket`
187-
:returns: A bucket with a connection using the provided credentials.
188-
"""
189-
connection = get_connection(project)
190-
return connection.get_bucket(bucket_name)

gcloud/storage/acl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
>>> from gcloud import storage
2222
>>> connection = storage.get_connection(project)
23-
>>> bucket = connection.get_bucket(bucket_name)
23+
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
2424
>>> acl = bucket.acl
2525
2626
Adding and removing permissions can be done with the following methods
@@ -427,8 +427,8 @@ def save(self, acl=None):
427427
You can use this to set access controls to be consistent from
428428
one bucket to another::
429429
430-
>>> bucket1 = connection.get_bucket(bucket1_name)
431-
>>> bucket2 = connection.get_bucket(bucket2_name)
430+
>>> bucket1 = storage.get_bucket(bucket1_name, connection=connection)
431+
>>> bucket2 = storage.get_bucket(bucket2_name, connection=connection)
432432
>>> bucket2.acl.save(bucket1.acl)
433433
434434
:type acl: :class:`gcloud.storage.acl.ACL`, or a compatible list.

gcloud/storage/api.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def lookup_bucket(bucket_name, connection=None):
5454
connection = get_default_connection()
5555

5656
try:
57-
return connection.get_bucket(bucket_name)
57+
return get_bucket(bucket_name, connection=connection)
5858
except NotFound:
5959
return None
6060

@@ -84,6 +84,77 @@ def get_all_buckets(connection=None):
8484
return iter(_BucketIterator(connection=connection))
8585

8686

87+
def get_bucket(bucket_name, connection=None):
88+
"""Get a bucket by name.
89+
90+
If the bucket isn't found, this will raise a
91+
:class:`gcloud.storage.exceptions.NotFound`.
92+
93+
For example::
94+
95+
>>> from gcloud import storage
96+
>>> from gcloud.exceptions import NotFound
97+
>>> try:
98+
>>> bucket = storage.get_bucket('my-bucket')
99+
>>> except NotFound:
100+
>>> print 'Sorry, that bucket does not exist!'
101+
102+
This implements "storage.buckets.get".
103+
104+
:type bucket_name: string
105+
:param bucket_name: The name of the bucket to get.
106+
107+
:type connection: :class:`gcloud.storage.connection.Connection` or
108+
``NoneType``
109+
:param connection: Optional. The connection to use when sending requests.
110+
If not provided, falls back to default.
111+
112+
:rtype: :class:`gcloud.storage.bucket.Bucket`
113+
:returns: The bucket matching the name provided.
114+
:raises: :class:`gcloud.exceptions.NotFound`
115+
"""
116+
if connection is None:
117+
connection = get_default_connection()
118+
119+
bucket_path = Bucket.path_helper(bucket_name)
120+
response = connection.api_request(method='GET', path=bucket_path)
121+
return Bucket(properties=response, connection=connection)
122+
123+
124+
def create_bucket(bucket_name, connection=None):
125+
"""Create a new bucket.
126+
127+
For example::
128+
129+
>>> from gcloud import storage
130+
>>> storage.set_defaults()
131+
>>> bucket = storage.create_bucket('my-bucket')
132+
>>> print bucket
133+
<Bucket: my-bucket>
134+
135+
This implements "storage.buckets.insert".
136+
137+
:type bucket_name: string
138+
:param bucket_name: The bucket name to create.
139+
140+
:type connection: :class:`gcloud.storage.connection.Connection` or
141+
``NoneType``
142+
:param connection: Optional. The connection to use when sending requests.
143+
If not provided, falls back to default.
144+
145+
:rtype: :class:`gcloud.storage.bucket.Bucket`
146+
:returns: The newly created bucket.
147+
:raises: :class:`gcloud.exceptions.Conflict` if
148+
there is a confict (bucket already exists, invalid name, etc.)
149+
"""
150+
if connection is None:
151+
connection = get_default_connection()
152+
153+
response = connection.api_request(method='POST', path='/b',
154+
data={'name': bucket_name})
155+
return Bucket(properties=response, connection=connection)
156+
157+
87158
class _BucketIterator(Iterator):
88159
"""An iterator listing all buckets.
89160

gcloud/storage/bucket.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def get_blob(self, blob):
171171
172172
>>> from gcloud import storage
173173
>>> connection = storage.get_connection(project)
174-
>>> bucket = connection.get_bucket('my-bucket')
174+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
175175
>>> print bucket.get_blob('/path/to/blob.txt')
176176
<Blob: my-bucket, /path/to/blob.txt>
177177
>>> print bucket.get_blob('/does-not-exist.txt')
@@ -302,7 +302,7 @@ def delete(self, force=False):
302302
# Ignore 404 errors on delete.
303303
self.delete_blobs(blobs, on_error=lambda blob: None)
304304

305-
self.connection.delete_bucket(self.name)
305+
self.connection.api_request(method='DELETE', path=self.path)
306306

307307
def delete_blob(self, blob):
308308
"""Deletes a blob from the current bucket.
@@ -315,7 +315,7 @@ def delete_blob(self, blob):
315315
>>> from gcloud.exceptions import NotFound
316316
>>> from gcloud import storage
317317
>>> connection = storage.get_connection(project)
318-
>>> bucket = connection.get_bucket('my-bucket')
318+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
319319
>>> print bucket.get_all_blobs()
320320
[<Blob: my-bucket, my-file.txt>]
321321
>>> bucket.delete_blob('my-file.txt')
@@ -397,7 +397,7 @@ def upload_file(self, filename, blob=None):
397397
398398
>>> from gcloud import storage
399399
>>> connection = storage.get_connection(project)
400-
>>> bucket = connection.get_bucket('my-bucket')
400+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
401401
>>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
402402
>>> print bucket.get_all_blobs()
403403
[<Blob: my-bucket, remote-text-file.txt>]
@@ -408,7 +408,7 @@ def upload_file(self, filename, blob=None):
408408
409409
>>> from gcloud import storage
410410
>>> connection = storage.get_connection(project)
411-
>>> bucket = connection.get_bucket('my-bucket')
411+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
412412
>>> bucket.upload_file('~/my-file.txt')
413413
>>> print bucket.get_all_blobs()
414414
[<Blob: my-bucket, my-file.txt>]
@@ -440,7 +440,7 @@ def upload_file_object(self, file_obj, blob=None):
440440
441441
>>> from gcloud import storage
442442
>>> connection = storage.get_connection(project)
443-
>>> bucket = connection.get_bucket('my-bucket')
443+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
444444
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
445445
>>> print bucket.get_all_blobs()
446446
[<Blob: my-bucket, remote-text-file.txt>]
@@ -451,7 +451,7 @@ def upload_file_object(self, file_obj, blob=None):
451451
452452
>>> from gcloud import storage
453453
>>> connection = storage.get_connection(project)
454-
>>> bucket = connection.get_bucket('my-bucket')
454+
>>> bucket = storage.get_bucket('my-bucket', connection=connection)
455455
>>> bucket.upload_file(open('~/my-file.txt'))
456456
>>> print bucket.get_all_blobs()
457457
[<Blob: my-bucket, my-file.txt>]
@@ -706,7 +706,7 @@ def configure_website(self, main_page_suffix=None, not_found_page=None):
706706
707707
>>> from gcloud import storage
708708
>>> connection = storage.get_connection(project)
709-
>>> bucket = connection.get_bucket(bucket_name)
709+
>>> bucket = storage.get_bucket(bucket_name, connection=connection)
710710
>>> bucket.configure_website('index.html', '404.html')
711711
712712
You probably should also make the whole bucket public::

0 commit comments

Comments
 (0)