刘凡 d0fdd49032 add S3 | il y a 2 ans | |
---|---|---|
.. | ||
.github | il y a 2 ans | |
tests | il y a 2 ans | |
.envrc | il y a 2 ans | |
.gitignore | il y a 2 ans | |
.prospector.yaml | il y a 2 ans | |
LICENSE | il y a 2 ans | |
README.md | il y a 2 ans | |
bucketstore.py | il y a 2 ans | |
default.nix | il y a 2 ans | |
requirements.dev.txt | il y a 2 ans | |
requirements.txt | il y a 2 ans | |
setup.cfg | il y a 2 ans | |
setup.py | il y a 2 ans | |
tox.ini | il y a 2 ans |
bucketstore is a very simple Amazon S3 client, written in Python. It aims to be much more straight-forward to use than boto3, and specializes only in Amazon S3, ignoring the rest of the AWS ecosystem.
AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
,
and AWS_DEFAULT_REGION
environment variables.pip install bucketstore
import bucketstore
# Create the bucket if it doesn't exist.
bucket = bucketstore.get('bucketstore-playground', create=True)
>>> bucket
<S3Bucket name='bucketstore-playground'>
# get/set using array syntax
>>> bucket['foo'] = 'bar'
>>> bucket['foo']
bar
# get/set using methods
>>> bucket.set('foo2', 'bar2')
>>> bucket.get('foo2')
bar2
# list keys
>>> bucket.list()
[u'foo', u'foo2']
# all keys
>>> bucket.all()
[<S3Key name=u'foo' bucket='bucketstore-playground'>, <S3Key name=u'foo2' bucket='bucketstore-playground'>]
# check if a key exists in the bucket
>>> 'foo' in bucket
True
# delete keys in the bucket
>>> del bucket['foo2']
{}
>>> bucket.key('foo')
<S3Key bucket='bucketstore-playground' name=u'foo'>
>>> foo = _
>>> foo.set('new value')
# Generate a temporary share URL.
>>> foo.temp_url(duration=1200)
u'https://bucketstore-playground.s3.amazonaws.com/foo?AWSAccessKeyId=AKIAI2RVFNXIW7WS66QQ&Expires=1485493909&Signature=L3gD9avwQZQO1i11dIJXUiZ7Nx8%3D'
# Make key publically accessable.
>>> foo.make_public()
>>> foo.url
'https://s3.amazonaws.com/bucketstore-playground/foo'
# Get / set metadata for key.
>>> foo.meta = {'foo': 'bar'}
>>> foo.meta
{'foo': 'bar}
# Rename key to 'foo3'.
>>> foo.rename('foo3')
# Delete the key.
>>> foo.delete()
# Create a key with a content type
>>> foo = bucket.key('foo.html')
>>> foo.set('<h1>bar</h1>', content_type='text/html')
# upload to key
>>> bucket.key('test.py').upload('/tmp/test.py')
# or upload with a file-like object! (make sure it's open in binary mode)
>>> with open('/tmp/test.py', 'rb') as file:
>>> bucket.key('test.py').upload(file)
# download to file
>>> bucket.key('test.py').download('/tmp/test.py')
# or download to a file-like object! (make sure it's open in binary mode)
>>> with open('/tmp/test.py', 'wb') as file:
>>> bucket.key('test.py').download(file)
# size of key
>>> bucket.key('test.py').size()
>>> len(bucket.key('test.py'))
15
Other methods include bucketstore.login(access_key_id, secret_access_key)
, bucketstore.list()
, and
bucketstore.get(bucket_name, create=False)
.
Tests are run through Tox.
# Run tests against all environments.
$ tox
# Run against a specific version.
$ tox -e py310
# Run with pytest arguments.
$ tox -- --pdb
✨🍰✨