Pre-signed URLs
Upload Flow
1. Client requests upload URL
2. Server generates pre-signed URL (S3)
3. Client uploads directly to S3
4. S3 notifies (optional)
5. Client confirms upload
Generate Pre-signed URL
@GetMapping("/upload-url")
public ResponseEntity<?> getUploadUrl(@RequestParam String filename) {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(
"my-bucket", filename)
.withMethod(HttpMethod.PUT)
.withExpiration(new Date(System.currentTimeMillis() + 300000));
URL url = s3Client.generatePresignedUrl(request);
return ResponseEntity.ok(Map.of("uploadUrl", url.toString()));
}
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding Pre-signed URLs is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Pre-signed URLs in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Pre-signed URLs implementation
// Key aspects: validation, error handling, logging, testing
public class PresignedURLs {
// Production-ready implementation
}Identify and handle edge cases for Pre-signed URLs. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Pre-signed URLs. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. Pre-signed URL allows?
2. Pre-signed URL expiration should be?
3. What is the primary purpose of Pre-signed URLs?
4. What is a common mistake when implementing Pre-signed URLs?
Flashcards
Question
Pre-signed URL?
Click to reveal answer
Answer
Limited-time access for specific S3 operation
Question
Expiration?
Click to reveal answer
Answer
5-15 minutes for security
Question
What is Pre-signed URLs?
Click to reveal answer
Answer
Pre-signed URLs is a key concept in backend development.
Question
When to use Pre-signed URLs?
Click to reveal answer
Answer
Use Pre-signed URLs when building production systems that require reliability, scalability, and maintainability.
Question
Pre-signed URLs best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Pre-signed URLs: limited-time, specific-operation access
- 2.Client uploads directly to S3
- 3.Keep expiration short (5-15 min)
- 4.Use for large file uploads
Interview Tips
- •Generate pre-signed URLs
- •Handle security
Cheat Sheet
Pre-signed URLs
- Limited-time, specific-operation access
- Client uploads directly to S3
- Expiration: 5-15 minutes
- Use: large file uploads