A robust and extensible file system abstraction layer that supports both local file system and S3 storage backends.
- Abstraction layer for file system operations
- Support for local file system and Amazon S3
- REST API for all operations
- Comprehensive file metadata
- Health monitoring
- Configurable through properties
- Logging and error handling
- Create files and directories
- Read and write files
- Append to files (local file system only)
- Move and rename files/directories
- Delete files/directories
- List directory contents (with optional recursion)
- Get file metadata
- Health checks
- Java 21 (LTS) or higher
- Gradle 8.5 or higher
- Docker (optional, for containerization)
- AWS credentials (for S3 storage)
The application can be configured through application.properties
:
# Choose storage backend (local or s3)
filesystem.type=local
filesystem.basePath=/path/to/storage
# S3 Configuration (if using S3)
filesystem.s3.region=us-west-2
filesystem.s3.accessKey=your-access-key
filesystem.s3.secretKey=your-secret-key
./gradlew clean build
./gradlew bootRun
Or run the JAR directly:
java -jar build/libs/fs-handler-1.0.jar
POST /api/v1/fs/file?path={path}
- Create a filePOST /api/v1/fs/directory?path={path}
- Create a directoryGET /api/v1/fs/file?path={path}
- Read a filePOST /api/v1/fs/file?path={path}
- Write to a file (multipart/form-data)GET /api/v1/fs/list?path={path}&recursive={true|false}&filter={pattern}
- List directory contentsDELETE /api/v1/fs/delete?path={path}
- Delete a file/directoryPUT /api/v1/fs/move?sourcePath={source}&destinationPath={destination}
- Move a file/directoryPUT /api/v1/fs/rename?path={path}&newName={newName}
- Rename a file/directoryGET /api/v1/fs/health
- Check system health
All endpoints return a FileMetadata
object (except health check) with the following structure:
{
"path": "string",
"name": "string",
"type": "FILE|DIRECTORY",
"size": "number",
"lastModified": "timestamp",
"children": "number (for directories)"
}
The API uses standard HTTP status codes:
- 200: Success
- 204: Success (for delete operations)
- 400: Bad Request (invalid path, invalid name)
- 404: Not Found
- 500: Internal Server Error
Error responses have the following format:
Error: <error message>
All paths are validated to prevent:
- Null or empty paths
- Path traversal attacks (..)
- Invalid characters in file names (/ in new names)
The File System Handler enforces strict path validation rules to ensure security:
-
Path Requirements
- Paths must not be null or empty
- Only relative paths are allowed
- Use "." to refer to the root directory
- All paths are relative to the configured base directory (
filesystem.basePath
)
-
Prohibited Paths
- Absolute paths (starting with "/" or "\")
- Windows-style absolute paths (e.g., "C:\folder")
- Path traversal attempts (containing "..")
- Mixed separator path traversal (e.g., "folder/../..\etc")
-
Examples
- Valid paths:
. folder folder/subfolder folder/file.txt
- Invalid paths:
/ /etc \temp C:\Windows ../folder folder/../../etc
- Valid paths:
-
Error Handling
- Invalid paths will result in a 400 Bad Request response
- Error message: "Invalid path: absolute paths and path traversal not allowed"
- All file operations (list, create, read, write, delete) enforce these rules
-
Security Notes
- All paths are normalized before processing
- The base directory (
filesystem.basePath
) acts as a chroot-like environment - No access is allowed outside the base directory
- Both Unix and Windows path separators are checked
The application can be run as a Docker container. Both Dockerfile and docker-compose.yml are provided.
-
Build the Docker image:
docker build -t fs-handler .
-
Run the container:
docker run -p 8080:8080 -v fs-data:/data/fs-handler fs-handler
-
Start the service:
docker-compose up -d
-
View logs:
docker-compose logs -f
-
Stop the service:
docker-compose down
The following environment variables can be configured:
Variable | Description | Default |
---|---|---|
SERVER_PORT | Application port | 8080 |
FILESYSTEM_TYPE | Storage type (local/s3) | local |
FILESYSTEM_BASEPATH | Base path for storage | /data/fs-handler |
FILESYSTEM_ROOT | Root directory | . |
fs-data
: Persistent volume for storing files- Mount point:
/data/fs-handler
The container includes a health check that:
- Pings
/api/v1/fs/health
endpoint - Runs every 30 seconds
- Has a 40-second startup grace period
- Retries 3 times before marking unhealthy
- Path traversal protection
- Configurable through Spring Security (not included)
- AWS credentials management for S3
- Buffered I/O operations
- Streaming for large files
- Configurable connection pools and timeouts
- Comprehensive error messages
- Proper HTTP status codes
- Detailed logging
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request