# S3 Fetch with Boto3

A Python3 script using boto3 to fetch data from Amazon S3 with hardcoded default credentials.

## Features

- **S3Fetcher Class**: Complete S3 interaction functionality
- **Hardcoded Credentials**: Uses [default] profile credentials hardcoded in the script
- **Multiple Operations**: List buckets, list objects, download files, get content, retrieve metadata
- **Error Handling**: Comprehensive error handling and logging
- **Examples**: Multiple usage examples and patterns

## Installation

1. Install required dependencies:
```bash
pip install -r requirements.txt
```

## Usage

### Basic Usage

```python
from s3_fetch import S3Fetcher

# Initialize with hardcoded default credentials
fetcher = S3Fetcher()

# List all buckets
buckets = fetcher.list_buckets()
print(f"Available buckets: {buckets}")

# List objects in a bucket
objects = fetcher.list_objects("my-bucket")

# Download a file
success = fetcher.download_file("my-bucket", "path/to/file.txt", "local_file.txt")

# Get object content as string
content = fetcher.get_object_content("my-bucket", "path/to/file.txt")

# Get object metadata
metadata = fetcher.get_object_metadata("my-bucket", "path/to/file.txt")
```

### Running the Demo

```bash
python s3_fetch.py
```

### Running Examples

```bash
python s3_config_example.py
```

## Configuration

The script uses hardcoded AWS credentials in the `S3Fetcher` class:

```python
self.aws_access_key_id = "AKIAIOSFODNN7EXAMPLE"
self.aws_secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
self.aws_region = "us-east-1"
```

**⚠️ Important**: Replace these example credentials with your actual AWS credentials before using in production.

## Available Methods

### S3Fetcher Class Methods

- `list_buckets()`: List all accessible S3 buckets
- `list_objects(bucket_name, prefix="")`: List objects in a bucket with optional prefix filter
- `download_file(bucket_name, object_key, local_file_path)`: Download file to local filesystem
- `get_object_content(bucket_name, object_key)`: Get object content as string
- `get_object_metadata(bucket_name, object_key)`: Get object metadata information

## Error Handling

The script includes comprehensive error handling for:
- AWS credential issues
- Network connectivity problems
- S3 access permissions
- File encoding issues
- Invalid bucket/object names

## Logging

The script uses Python's logging module to provide detailed information about operations:
- INFO level: Successful operations and general information
- ERROR level: Failed operations and error details

## Security Notes

- The example credentials in the code are AWS documentation examples and are not valid
- Replace with your actual AWS credentials before use
- Consider using AWS IAM roles or environment variables for production deployments
- Never commit real AWS credentials to version control

## Requirements

- Python 3.6+
- boto3 >= 1.26.0
- botocore >= 1.29.0

## License

This project is licensed under the Apache License 2.0 - see the license header in each file for details.