DEV Community

Gittech
Gittech

Posted on • Originally published at gittech.site on

S3Dict – Access S3 buckets as dictionaries / Powered by gittech.site 💗

S3Dict - Access S3 buckets as dictionaries

Install

pip install s3dict

Enter fullscreen mode Exit fullscreen mode

Usage

import boto3
import s3dict
s3dict.enable()

# create a boto3 bucket resource
bucket = boto3.resource( **s3** ).Bucket('s3dict-test')

# S3 object with key 'hello' is created, with content 'world' (pickled)
bucket['hello'] = 'world'
bucket['hola'] = 'mundo'

for k, v in bucket.items():
    print(f'{k} -> {v}')

del bucket['hello']

Enter fullscreen mode Exit fullscreen mode

Why oh why?

Just for curiosity.

bucket[k] = v is easier than bucket.put_object(Key=k, Body=pickle.dumps(v)) right? :)

Please let us know if you found a real use case.

Limitations

  • Buckets are not ordered like Python dictionaries are ordered (by insertion order).
  • len(bucket) runs O(N) - it lists bucket objects and counts.
  • popitem() returns an arbitrary item (since unordered).
  • keys() and values() are iterators, not views.
  • Dictionary keys must be str.

They cannot be too long (underlying S3 key may not be >1024 chars).

  • Dictionary values must be serializable (Pickle by default, you can bring your own serialization / codec ).

Contributions

They are welcome.

TODOs offers some ideas.

This response is generated by: https://gittech.site

On here: https://gittech.site/github/item/42427105

Top comments (0)