This project provides a secure and efficient service for uploading files to Amazon S3 with authentication and database integration.
The S3 File Upload Service is designed to handle file uploads to Amazon S3 buckets while incorporating user authentication and maintaining upload records in a database. It offers a robust API for file management, leveraging Go's concurrency features for optimal performance.
Key features include:
- Secure file uploads to Amazon S3
- User authentication for upload requests
- Database integration for tracking upload metadata
- Efficient handling of large file uploads
- Configurable S3 client for flexible deployment
auth/
: Contains authentication-related codeconsumer.go
: Implements authentication logic
internal/
: Internal packagescommon/
: Shared utilitiesjson.go
: JSON handling functionsvalidator.go
: Input validation functions
database/
: Database interactiondb.go
: Database connection and query executionmodels.go
: Database modelsuploadFile.sql.go
: SQL queries for file uploads
main.go
: Entry point of the applicationmiddleware/
: HTTP middlewareauth.go
: Authentication middleware
s3client/
: Amazon S3 client implementationmain.go
: S3 client setup and operations
s3UploadFile/
: File upload handlinghandlers.go
: HTTP handlers for file uploadsmodels.go
: Data models for file uploads
sql/
: SQL-related filesqueries/
: SQL query filesuploadFile.sql
: SQL queries for file operations
schema/
: Database schema0001_uploads.sql
: Initial schema for uploads table
sqlc.yaml
: Configuration file for sqlc code generation
- Ensure you have Go 1.16 or later installed.
- Clone the repository:
git clone <repository-url> cd <repository-directory>
- Install dependencies:
go mod download
- Set up your AWS credentials in
~/.aws/credentials
or use environment variables. - Configure the database connection in
internal/database/db.go
. - Adjust S3 bucket settings in
s3client/main.go
.
- Build the application:
go build -o s3uploader
- Run the service:
./s3uploader
POST /upload
Authorization: Bearer <token>
Content-Type: multipart/form-data
file: <file-data>
Response:
{
"file_id": "123e4567-e89b-12d3-a456-426614174000",
"file_name": "example.jpg",
"file_size": 1024000,
"upload_time": "2023-04-01T12:00:00Z"
}
Run the test suite:
go test ./...
- If you encounter S3 access issues, verify your AWS credentials and bucket permissions.
- For database connection problems, check the connection string and ensure the database server is running.
- Enable debug logging by setting the
DEBUG
environment variable totrue
.
- Client sends an authenticated file upload request.
- Authentication middleware validates the user token.
- Upload handler receives the file and metadata.
- File is streamed to S3 using the S3 client.
- Upload metadata is stored in the database.
- Response with file details is sent back to the client.
Client -> Auth Middleware -> Upload Handler -> S3 Client -> S3 Bucket
-> Database
The project uses the following AWS resources:
- S3 Bucket: Stores uploaded files
- RDS PostgreSQL: Stores upload metadata and user information
Note: Specific infrastructure details are not provided in the given context. For production deployment, consider using infrastructure-as-code tools like Terraform or AWS CloudFormation to manage these resources.