Abstraction for easy access to AWS S3.
Project description
AWS S3 Helper Package 🇬🇧
Description
aws-s3-helper is a Python utility that provides a simplified interface for interacting with AWS S3. It provides functions for uploading, downloading, reading, deleting, and renaming files and folders, as well as managing buckets.
Features
- Listing Buckets, Folders, and Files: List all the buckets in your S3 account, or list the contents of a specific folder in an S3 bucket.
- File and Folder Upload: Upload individual files or entire folders to an S3 bucket with a progress bar.
- File and Folder Download: Download individual files or folders from an S3 bucket with progress bar support.
- Reading Files in S3: Reads the contents of a file in S3 without the need to download the file.
- File and Folder Deletion: Deletes files or folders from an S3 bucket.
- File and Folder Renaming: Rename files or folders in an S3 bucket.
- Bucket Management: Create, delete, and rename S3 buckets.
Installation
You can install the package directly from PyPi using pip:
pip3 install aws-s3-helper
Usage
Initial configuration
To get started, it is recommended to have an .env file in the root directory of your project with your AWS credentials:
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_REGION=your_region
Example of use
from dotenv import dotenv_values
from aws_s3_helper.s3 import S3
# Create an instance of the S3 class
env = dotenv_values()
s3 = S3(
aws_access_key_id=env["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=env["AWS_SECRET_ACCESS_KEY"],
aws_region=env["AWS_REGION"],
)
# Bucket name
bucket_name = "your-bucket"
# List all buckets in your S3 account
buckets = s3.list_buckets()
print("Buckets:", buckets)
# List the contents of a folder in S3
listing = s3.list(bucket_name, "path_in_s3")
print("Folders:", listing["folders"])
print("Files:", listing["files"])
# Upload a folder to S3
s3.upload_folder(bucket_name, "local_folder", "path_to_s3/")
# Read a file in S3 without downloading it
content = s3.read_file(bucket_name, "path_in_s3/file.txt")
print(content)
# Download a folder from S3
s3.download_folder(bucket_name, "path_in_s3/", "local_folder/")
# Rename a file in S3
s3.rename_file(bucket_name, "path_in_s3/old_name.txt", "path_in_s3/new_name.txt")
# Rename a folder in S3
s3.rename_folder(bucket_name, "path_in_s3/old_folder/", "path_in_s3/new_folder/")
# Create a new bucket
s3.create_bucket("new-bucket")
# Rename a bucket
s3.rename_bucket("old-bucket-name", "new-bucket-name")
# Delete a folder in S3
s3.delete_folder(bucket_name, "path_in_s3/")
# Delete a bucket
s3.delete_bucket("bucket-to-delete")
Running tests
To run the tests, follow these steps:
-
Ensure you have a valid AWS configuration by setting up an .env file as described above.
-
Install the necessary dependencies, which include unittest, boto3, and python-dotenv if they are not already installed:
pip3 install boto3 python-dotenv
-
Run the tests using the following command:
python3 -m unittest discover -s test
This command will automatically discover and run all test cases in the test directory, checking the upload, download, read, delete, rename, and listing operations on S3, as well as bucket management functions.
License
This project is licensed under the GNU GPLv3 license. See the LICENSE file for more details.
Contact
For any question or suggestion, feel free to open an issue in the repository or contact me directly.
Paquete AWS S3 Helper 🇪🇸
Descripción
aws-s3-helper es una utilidad de Python que proporciona una interfaz simplificada para interactuar con AWS S3. Ofrece funciones para subir, descargar, leer, y eliminar archivos y carpetas.
Características
- Subida de Archivos y Carpetas: Sube archivos individuales o carpetas enteras a un bucket de S3 con una barra de progreso.
- Descarga de Archivos y Carpetas: Descarga archivos individuales o carpetas desde un bucket de S3 con soporte de barra de progreso.
- Lectura de Archivos en S3: Lee el contenido de un archivo en S3 sin necesidad de descargarlo.
- Eliminación de Archivos y Carpetas: Elimina archivos o carpetas de un bucket de S3.
- Gestión de Buckets: Crea, elimina, renombra y lista buckets de S3.
- Listar Buckets, Carpetas y Archivos: Lista todos los buckets en tu cuenta de S3, o lista el contenido de una carpeta específica en un bucket de S3.
Instalación
Puedes instalar el paquete directamente desde PyPi usando pip:
pip3 install aws-s3-helper
Uso
Configuración Inicial
Para comenzar, es recomendable tener un archivo .env en el directorio raíz de tu proyecto con tus credenciales de AWS:
AWS_ACCESS_KEY_ID=tu_access_key_id
AWS_SECRET_ACCESS_KEY=tu_secret_access_key
AWS_REGION=tu_region
Ejemplo de uso
from dotenv import dotenv_values
from aws_s3_helper.s3 import S3
# Crea una instancia de la clase S3
env = dotenv_values()
s3 = S3(
aws_access_key_id=env["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=env["AWS_SECRET_ACCESS_KEY"],
aws_region=env["AWS_REGION"],
)
# Nombre del bucket
bucket_name = "tu-bucket"
# Listar todos los buckets en tu cuenta de S3
buckets = s3.list_buckets()
print("Buckets:", buckets)
# Listar el contenido de una carpeta en S3
listado = s3.list(bucket_name, "ruta_en_s3")
print("Carpetas:", listado["folders"])
print("Archivos:", listado["files"])
# Subir una carpeta a S3
s3.upload_folder(bucket_name, "carpeta_local", "ruta_en_s3/")
# Leer un archivo en S3 sin descargarlo
contenido = s3.read_file(bucket_name, "ruta_en_s3/archivo.txt")
print(contenido)
# Descargar una carpeta desde S3
s3.download_folder(bucket_name, "ruta_en_s3/", "carpeta_local/")
# Renombrar un archivo en S3
s3.rename_file(bucket_name, "ruta_en_s3/viejo_nombre.txt", "ruta_en_s3/nuevo_nombre.txt")
# Renombrar una carpeta en S3
s3.rename_folder(bucket_name, "ruta_en_s3/carpeta_vieja/", "ruta_en_s3/carpeta_nueva/")
# Crear un nuevo bucket
s3.create_bucket("nuevo-bucket")
# Renombrar un bucket
s3.rename_bucket("bucket-viejo-nombre", "bucket-nuevo-nombre")
# Eliminar una carpeta en S3
s3.delete_folder(bucket_name, "ruta_en_s3/")
# Eliminar un bucket
s3.delete_bucket("bucket-a-eliminar")
Ejecución de test
Para ejecutar los tests, sigue estos pasos:
-
Asegúrate de que tienes una configuración de AWS válida mediante la creación de un archivo .env como se describe anteriormente.
-
Instala las dependencias necesarias, que incluyen unittest, boto3, y python-dotenv si aún no están instaladas:
pip3 install boto3 python-dotenv
-
Ejecuta los tests con el siguiente comando:
python3 -m unittest discover -s test
Este comando descubrirá y ejecutará automáticamente todos los casos de prueba en el directorio test, comprobando las operaciones de subida, descarga, lectura, eliminación, renombrado y listado en S3, así como las funciones de gestión de buckets.
Licencia
Este proyecto está licenciado bajo la licencia GNU GPLv3. Consulta el archivo LICENSE para más detalles.
Contacto
Para cualquier consulta o sugerencia, no dudes en abrir un issue en el repositorio o contactarme directamente.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws_s3_helper-1.2.0.tar.gz.
File metadata
- Download URL: aws_s3_helper-1.2.0.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d1778bb5be715e398fcfe7f129b80ebe79959f084cce9f29314b4318ec19496
|
|
| MD5 |
469d92f1a28139179c760709d130471a
|
|
| BLAKE2b-256 |
d8983c7912866d173bbf2ed07b8a0192d2deaefc372ef80b9ca16d37a4847e24
|
File details
Details for the file aws_s3_helper-1.2.0-py3-none-any.whl.
File metadata
- Download URL: aws_s3_helper-1.2.0-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d58ee93371c0002d5301bea08c27b80e7015090c703818d01edc5d5527b06e57
|
|
| MD5 |
7675fd32d1630bf22c47fe2e3cd5994f
|
|
| BLAKE2b-256 |
aa10725876b6742dcd288a9c43fdeb10016ee637b50cdadae29b20141f64c183
|