S3-Storage
Detailed documentation and implementation guides.
S3 File Service Documentation
Overview
The S3 File Service provides a robust interface for handling file uploads and storage using AWS S3. This service is implemented in the S3FileService class and uses a fluent interface for easy file handling and metadata configuration.
Configuration
Environment Variables
Add these variables to your .env file:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=your_region
AWS_BUCKET=your_bucket_name
Usage Example
use Modules\Attachment\Service\S3FileService;
// Upload a file with metadata using fluent interface
S3FileService::file($request->file('document'))
->display_name('Custom Display Name')
->type_id(1)
->type_name('document')
->model('App\Models\Document')
->model_id(123)
->path('custom/path')
->store();
// Delete a file
S3FileService::delete('path/to/file', 'filename.ext');
Field Requirements
Required Fields
These fields MUST be set before calling store():
file: The uploaded file (passed to::file()method)model: The fully qualified class name of the related modelmodel_id: The ID of the related model recordtype_id: The type identifiertype_name: The name of the type
Optional Fields
display_name: Custom display name- If not set, defaults to the original filename
path: Custom storage path- If not set, defaults to 'attachments'
Auto-Generated Fields
These fields are automatically handled by the service:
fileName: Generated using random string + timestamp + extensionextension: Extracted from the original fileoriginalName: Taken from the uploaded file
Key Features
- Fluent interface for easy configuration
- Secure file name generation
- Automatic file metadata extraction
- Required field validation
- AWS S3 integration
- Comprehensive error handling
Error Handling
The service throws S3FileServiceException in the following cases:
- Invalid file upload
- Missing required fields (model, model_id, type_id, type_name)
- S3 storage failures
- File not found (when deleting)
- General storage errors
Best Practices
- Always wrap
store()anddelete()calls in try-catch blocks - Use meaningful type names and IDs for better organization
- Group related files using the path option
- Validate file uploads before processing
- Keep track of stored file paths for future reference