Skip to main content

Access S3 objects like tree.

Project description

S3Tree 🌲

Build Status Coverage Status

S3Tree allows you to access files stored on Amazon AWS S3 as a simple directory tree. You can traverse the tree and read files as easily as you os.walk.

>>> import s3tree
>>> s3tree.config.aws_access_key_id = '<AWS-ACCESS-KEY>'
>>> s3tree.config.aws_secret_access_key = '<AWS-SECRET-KEY>'
>>> tree = s3tree.S3Tree(bucket_name='my-awesome-bucket')
>>> len(tree)
77
>>> for obj in tree: print(obj.name)
...
admin
assets
...
index.js

Installation

To install S3Tree, use pipenv (or pip):

$ pipenv install s3tree

Documentation

Using AWS credentials

The s3tree module exposes a config object that can be used to set the AWS credentials you want to use globally. You should ideally do this before you make any instances of S3Tree.

>>> import s3tree
>>> s3tree.config.aws_access_key_id = '<AWS-ACCESS-KEY>'
>>> s3tree.config.aws_secret_access_key = '<AWS-SECRET-KEY>'

Credentials can also be passed while creating an S3Tree instance:

>>> tree = s3tree.S3Tree('dummy-bucket', aws_access_key_id='<AWS-ACCESS-KEY>', aws_secret_access_key='<AWS-SECRET-KEY>')

Passing the credentials during instance creation overrides the global config.

Fetching a tree

The S3Tree object represents a tree at any given path. The path can be specified while creating a new tree. If no path, or / is passed, the root if the bucket is fetched.

>>> tree = s3tree.S3Tree(bucket_name='dummy')  # tree at the root of the bucket `dummy`
>>> tree = s3tree.S3Tree(bucket_name='dummy', path='/admin/css')  # tree under the path `/admin/css`
>>> tree = s3tree.S3Tree(bucket_name='dummy', path='admin/css')  # this works too.

The tree object above is an iterable that contains all files and directories in this tree. len(tree) gives you the total size of this tree. If you want to access files and directories separately:

>>> tree.directories  # iterable for all the directories in the tree
>>> tree.files  # iterable for all the files in the tree

The Directory object

Each element in tree.directories is a Directory object. This has attributes that help you display the directory in a human-friendly manner, and methods to fetch the tree under itself.

>>> mydir = tree.directories[0]
>>> mydir.name  # the name of this directory
css
>>> mydir.path  # the full path of this directory
/admin/css
>>> child_tree = mydir.get_tree()  # retrieve and store the tree under `mydir` to `child_tree`

The File object

Each element in tree.files is a File object, which has attributes and methods to display properties and read the file.

>>> myfile = tree.files[0]
>>> myfile.name  # name of this file
index.js
>>> myfile.size  # human-readable size of this file
4 KB
>>> contents = myfile.read()  # reads the file and stores its contents in `contents`

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

s3tree-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

s3tree-0.1.1-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file s3tree-0.1.1.tar.gz.

File metadata

  • Download URL: s3tree-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for s3tree-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5aab055e44ea317aaf174b4948885b9ee68858cb40d60a73ee3776589b2ee4e5
MD5 a3d9c9042cd9d49d1bb4016de3937e6d
BLAKE2b-256 5dffc7a401154ebc20c936a8958ce3b039869d769e23c083d820987cf32a3d18

See more details on using hashes here.

Provenance

File details

Details for the file s3tree-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for s3tree-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cfdbedd1584aec0a84dfe922c3d4ccce8ce7e8135eb8f0edd46d5a301419e79b
MD5 a324a9537580b49fbf42e36e6794fe7b
BLAKE2b-256 daaca0d6e1f7c32ea950bac2f036fd3004db7b5fa6f0d809a2c6f3fc65dff336

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page