Overview
Before initiating an upload directly to S3, S3M performs an authorization check against the currently authenticated user using Laravel’s policy system. This ensures only authorized users can upload files to your S3 bucket.How Authorization Works
S3M’s internal signed storage URL generator performs an authorization check before:- Creating a multipart upload session
- Generating presigned URLs for part uploads
- Completing the multipart upload
Setting Up Authorization
Create User Policy
If you don’t already have a This creates a new policy class at
UserPolicy, create one using the Artisan command:app/Policies/UserPolicy.php.Add uploadFiles Method
Add the
uploadFiles method to your UserPolicy class:This basic implementation allows all authenticated users to upload files. See the customization section below for more advanced authorization logic.
Customizing Authorization
TheuploadFiles method gives you complete control over who can upload files. Here are common authorization patterns:
Role-Based Authorization
Allow only users with specific roles to upload:Subscription-Based Authorization
Restrict uploads to users with active subscriptions:Storage Quota Authorization
Limit uploads based on user storage quota:Email Verification Required
Require email verification before allowing uploads:Complex Authorization Logic
Combine multiple conditions:Authorization Response
TheuploadFiles method should return:
true- Allow the uploadfalse- Deny the uploadResponse- Return a custom response (optional)
Custom Response Example
Middleware Integration
S3M routes automatically use the middleware defined inconfig/s3m.php. Ensure authentication middleware is included:
config/s3m.php
Testing Authorization
Test your authorization policy using Laravel’s policy testing features:tests/Feature/UploadAuthorizationTest.php
Controller Implementation
The authorization check is automatically performed in the S3MultipartController:src/Http/Controllers/S3MultipartController.php
Best Practices
Security Recommendations:
- Always require authentication for file uploads
- Implement rate limiting to prevent abuse
- Validate file types and sizes on the server
- Use email verification for user accounts
- Monitor storage usage and set quotas
- Log upload activities for audit trails
Handling Authorization Failures
When authorization fails, S3M returns an HTTP 403 Forbidden response. Handle this in your frontend:Advanced: Custom Authorization Per Upload
For more granular control, you can extend the policy to check specific upload parameters:The
uploadFiles policy method is the gatekeeper for all S3M uploads. Customize it to match your application’s security requirements.