Skip to content

Commit

Permalink
Initial commit 🐝
Browse files Browse the repository at this point in the history
  • Loading branch information
sotberd committed Mar 26, 2023
0 parents commit a018092
Show file tree
Hide file tree
Showing 8 changed files with 659 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish PyPI Package

on:
release:
types: [published]

jobs:
release_package:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Build a binary wheel and a source tarball.
run: pip install wheel && python setup.py sdist bdist_wheel

- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
164 changes: 164 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.vscode/
.env
.venv
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Code Lighthouse

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# s3hive

A tool built on top of boto3 that allows you to easily manage your S3 buckets.

<!-- Python version bugde -->

<!-- [![Python Version](https://img.shields.io/pypi/pyversions/s3hive.svg)](https://pypi.org/project/s3hive/)
[![PyPI version](https://badge.fury.io/py/s3hive.svg)](https://badge.fury.io/py/s3hive)
[![Build Status](https://travis-ci.com/sotberd/s3hive.svg?branch=main)](https://travis-ci.com/sotberd/s3hive)
[![codecov](https://codecov.io/gh/sotberd/s3hive/branch/main/graph/badge.svg)](https://codecov.io/gh/sotberd/s3hive) -->

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Overview

s3hive is a Python tool that provides a high-level interface for working with S3 buckets. With this tool, you can easily perform common operations on your S3 buckets such as creating, deleting, listing, uploading files, etc.

This tool uses the popular boto3 library to interact with the S3 API, making it simple and intuitive to use.

s3hive is designed to be easy to use, with a simple and consistent API that abstracts away many of the complexities of working with S3 buckets. Whether you're a seasoned developer or just getting started, s3hive can help you streamline your S3 operations and save time.

## Features

- Create a new S3 bucket
- Delete an existing S3 bucket
- Generate a presigned URL to share an S3 object
- List all S3 buckets
- Upload files to an S3 bucket
- Download files from an S3 bucket
- List files in an S3 bucket
- Delete files from an S3 bucket

This tool is a wrapper around the boto3 library. It provides a simple interface to manage your S3 buckets.

## Getting Started

### Installation

You can install s3hive using pip:

```bash
$ pip install s3hive
```

### Usage

Here's an example of how to use s3hive to list all your S3 buckets:

```python
import s3hive as s3
import os

ENDPOINT_URL = os.environ.get('ENDPOINT_URL')
REGION = os.environ.get('REGION')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')

s3hive = s3.Bucket(
endpoint_url=ENDPOINT_URL,
region=REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)

buckets = s3hive.list_buckets()

print(buckets)

# Output:
# [{
# 'name': 'bucket1',
# 'creation_date': datetime.datetime(2020, 5, 1, 12, 0, 0, tzinfo=tzutc())
# }]

```

For more examples and detailed documentation, please visit our [GitHub repository](https://github.com/sotberd/s3hive/blob/main/example.py).

### Methods

| Method | Description |
| :--------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_get_client()` | Get the S3 client. Returns a boto3 client object for the S3 service. |
| `create_bucket(bucket: str, acl: str = "private")` | Create an S3 bucket in a specified region. bucket is the name of the bucket to create, and acl is the access control list. Returns True if the bucket was created successfully, or raises an exception if an error occurs. |
| `delete_bucket(bucket: str) ` | Delete an S3 bucket. bucket is the name of the bucket to delete. Returns True if the bucket was deleted successfully, or raises an exception if an error occurs. |
| `list_buckets(names_only: bool = False)` | List all buckets in the S3 account. If names_only is True, return only the bucket names. Otherwise, return a list of dictionaries, with each dictionary containing the bucket name and creation date. Raises an exception if an error occurs. |
| `list_objects(bucket: str, keys_only: bool = False)` | List all objects in the specified bucket. If keys_only is True, return only the object keys. Otherwise, return a list of dictionaries, with each dictionary containing the object key, size, and last modified date. Raises an exception if an error occurs. |
| `create_presigned_url(bucket: str, key: str, expiration: int = 3600)` | Generate a presigned URL to share an S3 object. bucket is the name of the bucket containing the object, key is the object key, and expiration is the time in seconds for the presigned URL to remain valid. Returns the presigned URL as a string, or raises an exception if an error occurs. |
| `upload(bucket: str, file_name: str, key: str = None, extraArgs: dict = None, filesize: int = None)` | Upload an object to an S3 bucket. file_name is the path to the file to upload, bucket is the name of the bucket to upload to, key is the S3 object name. If not specified, then file_name is used. extraArgs is a dictionary of extra arguments that may be passed to the S3 API. Returns True if the file was uploaded successfully, or raises an exception if an error occurs. |
| `download(bucket: str, key: str, local_dir: str = ROOT_DIR)` | Download an object from S3 bucket to local directory. key is the S3 object key, and local_dir is the local directory to download the file to (if local_dir not provided object will stored on the root folder). Returns True if the file was downloaded successfully, or raises an exception if an error occurs. |
| `delete(bucket: str, key: str)` | Delete an object from an S3 bucket. bucket is the name of the bucket containing the object, and key is the object key. Returns True if the object was deleted successfully, or raises an exception if an error occurs. |

## License

s3hive is licensed under the [MIT License](https://opensource.org/license/mit/).
45 changes: 45 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import s3hive as s3
import os
from dotenv import load_dotenv

load_dotenv()

# Environment variables
ENDPOINT_URL = os.getenv('ENDPOINT_URL')
REGION = os.getenv('REGION')
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')

# Instantiate a Bucket object
s3hive = s3.Bucket(
endpoint_url=ENDPOINT_URL,
region=REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)


# List all buckets
buckets = s3hive.list_buckets()
print(buckets)

# List all objects in a bucket
objects = s3hive.list_objects('my-bucket')
print(objects)

# Create a presigned URL
url = s3hive.create_presigned_url('my-bucket', 'my-object')
print(url)

# Upload an object
uploaded = s3hive.upload('my-bucket', 'my-object.yml', 'my-file.yml')
print(uploaded)

# Download an object
downloaded = s3hive.download('my-bucket', 'my-file.yml', )
print(downloaded)

# Delete an object
marker, metadata = s3hive.delete('my-bucket', 'my-file.yml')
print(marker, metadata)

5 changes: 5 additions & 0 deletions s3hive/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .bucket import Bucket

__all__ = [
"Bucket"
]
Loading

0 comments on commit a018092

Please sign in to comment.