Skip to content

Raise warning/exception when user tries to get_bucket(x) if x includes forward slash #2956

@eilamg

Description

@eilamg

Hi there.

I made a silly mistake when trying to list blobs in a bucket, and have a proposal that could prevent users from repeating said mistake.

When initializing a Bucket object, I did so with a forward slash ("/") in the bucket name string. This produced a supposedly valid bucket, as seen here (actual bucket name changed for sake of demonstration):

>>> from gcloud import storage
>>> c = storage.Client()
>>> b0 = c.get_bucket('valid-bucket')
>>> b0.exists()
True
>>> b1 = c.get_bucket('valid-bucket/')  # <-- A silly mistake
>>> b1.exists()
True

While both methods of initialization produce seemingly valid Bucket objects, initializing with a forward slash breaks further on, i.e:

>>> len([x for x in b0.list_blobs()])
8112
>>> len([x for x in b1.list_blobs()])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "/usr/local/lib/python3.5/dist-packages/gcloud/iterator.py", line 79, in __iter__
    response = self.get_next_page_response()
  File "/usr/local/lib/python3.5/dist-packages/gcloud/iterator.py", line 115, in get_next_page_response
    method='GET', path=self.path, query_params=self.get_query_params())
  File "/usr/local/lib/python3.5/dist-packages/gcloud/connection.py", line 347, in api_request
    error_info=method + ' ' + url)
gcloud.exceptions.NotFound: 404 Not Found (GET https://www.googleapis.com/storage/v1/b/valid-bucket//o?projection=noAcl)

gcloud adds a second forward slash when constructing URLs internally, and this obviously breaks. I'd suggest raising a warning or exception when users try to initialize a bucket with a forward slash, or alternatively fixing it behind-the-scenes (simply dropping the slash).

Cheers,
Eilam

Metadata

Metadata

Labels

api: storageIssues related to the Cloud Storage API.priority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions