Skip to main content

S3Tree 🌲

Build Status Coverage Status Maintainability

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 S3Tree object can be easily represented as JSON:

>>> tree.as_json

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`
>>> json_data = mydir.as_json  # JSON representation of this directory

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`
>>> json_data = myfile.as_json  # JSON representation of this file obj

Metadata

Release files for s3tree 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for s3tree 0.3.0
File Size Uploaded
s3tree-0.3.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for s3tree 0.3.0
File Interpreter ABI Platform
s3tree-0.3.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 16.8 kB

Release files / s3tree-0.3.0.tar.gz

Download URL s3tree-0.3.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9e03d11e276420b684e1e403168fc1d87db844ca088a70db8ee3867502dfa6cd
BLAKE2b-256 checksum
How to use checksums
fbb3977d073a35a0db894e40cf42ddb457346f8ccf574b6c03fc23f09801c780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / s3tree-0.3.0-py2.py3-none-any.whl

Download URL s3tree-0.3.0-py2.py3-none-any.whl
Size 8.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b6e549459ec5d86badcae529786f4e3ca9c7dae608bff97c487a1c50bf9faf28
BLAKE2b-256 checksum
How to use checksums
50b5fbe9e438b1d131e485cc3cdb52c93be7b4f3815f91c5bab87bbdc47dcbba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page