GeezerVison is showing github.com — Consider making buckets dictionary-like for cloud storage · Issue #29 · googleapis/google-cloud-python
Consider making buckets dictionary-like for cloud storage #29DescriptionFor example
connection = get_connection()
bucket = connection['bucket-name']
key = bucket['/path/to/file.txt']
del key
This would be a neat abstraction, though it's certainly not necessary. Activitydhermes commented on Feb 10, 2015 RE: Our discussion today we want Connection to be stateless, so at best we want a Bucket to have __getitem__ defined (or to inherit from dict).
Though given the observations from @thobrla in #545, Allowing
blob = bucket['/remote/path/to/file.txt']
would obscure the local / network interactions. What's more, via implicit behavior we can make it easy to create a reference to a blob and check if it exists:
>>> blob = Blob('/remote/path/to/file.txt')
>>> blob.exists()
Truechanged the title [-]Consider making connections and buckets dictionary-like for cloud storage[/-] [+]Consider making buckets dictionary-like for cloud storage[/+] on Feb 10, 2015jgeewax commented on Feb 10, 2015 ContributorAuthor More actions I'm 👍 to giving buckets the __getitem__ type accessors.
However, I think the code I'd rather see is:
>>> blob = connection.blob('/remote/path/to/file.txt')
>>> blob.exists()
True
I can see how Blob('/path/to/file.txt') would rely on the default connection, but it feels a tiny bit fishy to me that it has to figure out which credentials / project to use. (See related -- and just created -- #613) tseaver commented on Feb 11, 2015 @jgeewax shouldn't your example read like so?
>>> blob = bucket.blob('/remote/path/to/file.txt')
>>> blob.exists()
Truejgeewax commented on Feb 11, 2015 ContributorAuthor More actions tseaver commented on Feb 26, 2015 OK, current status is:
storage.connection.Connection.get_bucket() incurs a network hit, and raises a NotFound on a miss.
storage.connection.Connection.create_bucket() incurs a network hit, and raises (Conflict, i.e. HTTP 409 status?) if the bucket exists already (API docs don't define error responses, and furthermore Carthago delenda est).
storage.bucket.Bucket.get_blob() incurs a network hit, and converts the NotFound on a miss to a None.
storage.bucket.Bucket.new_blob() does not incur a network hit.
@jgeewax: IIUC, you would like Connection.__getitem__() to return a new Bucket instance, without the network hit, and have Bucket.exists() do that lookup if. Likewise, you would like Bucket.__getitem__() to return a new Blob instance, again without the network hit, and have Blob.exists() do that lookup.
In this universe, I think Connection.get_bucket(), Connection.create_bucket(), Bucket.get_blob(), and Bucket.new_blob() would all disappear. Applications that wanted to ensure that a bucket or blob did or did not already exist would just call exits() and handle it themselves.
This mode is somewhat counter-intuitive for Python programmers, who might reasonably expect __getitem__() to raise a KeyError on a miss. Unfortunately, we have to choose: a) do what I just proposed, and be less "Pythonic"; b) incur the network lookup (more "Pythonic" but worse at scale), or c) work out a mode for caching the buckets / blobs locally, and live with the pain / complexity of trying to do cache invalidation correctly. dhermes commented on Feb 26, 2015 @tseaver We should move towards Connection being a low-level / power-user interface and put things in storage.api (like datastore.api). (RE: "Connection.__getitem__() to return a new Bucket")
Overall I don't think __getitem__ can be done in a way that won't confound some users (will simultaneously pleasing others) since it is such a core language feature.
I vote to close. added 8 commits that reference this issue on Mar 2, 2026This issue will close once commit 01e869f is merged into the 'main' branch.01e869f This issue will close once commit bc191e5 is merged into the 'main' branch.bc191e5 This issue will close once commit 9242000 is merged into the 'main' branch.9242000 This issue will close once commit b3cc28f is merged into the 'main' branch.b3cc28f This issue will close once commit 3c91fc0 is merged into the 'main' branch.3c91fc0 This issue will close once commit f0aebdb is merged into the 'main' branch.f0aebdb This issue will close once commit 86f6807 is merged into the 'main' branch.86f6807 This issue will close once commit d315293 is merged into the 'main' branch.d315293 added a commit that references this issue on Mar 31, 2026This issue will close once commit 2d80a0e is merged into the 'main' branch.2d80a0e
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|