Careeroza
Real World Example
Seller Image Uploads to Cloud Storage
Marketplaces like Amazon, Meesho and Flipkart let sellers upload many product photos. The upload API does not keep files on the server disk (servers are replaced and scaled all the time). It streams them to object storage and stores the key in the database; a CDN serves the images.
@Service
@RequiredArgsConstructor
public class S3ImageStorage {
private final S3Client s3; // AWS SDK v2
private final ProductImageRepository images;
@Value("${shopease.images.bucket}") private String bucket;
@Value("${shopease.images.cdn-url}") private String cdnUrl;
public ImageResponse store(Long productId, MultipartFile file) throws IOException {
String key = "products/%d/%s.webp".formatted(productId, UUID.randomUUID());
s3.putObject(PutObjectRequest.builder()
.bucket(bucket).key(key)
.contentType(file.getContentType())
.cacheControl("public, max-age=31536000, immutable")
.build(),
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
images.save(new ProductImage(productId, key, file.getOriginalFilename(), file.getSize()));
return new ImageResponse(key, file.getOriginalFilename(), file.getSize(), file.getContentType(), cdnUrl + "/" + key);
}
}Example
curl -F "[email protected]" -H "Authorization: Bearer eyJ..." localhost:8080/api/seller/products/88/imagesExample output
HTTP/1.1 201 Created
{"fileName":"products/88/4e1c7b2a-....webp","originalName":"kurta-blue.webp","size":91822,"contentType":"image/webp","url":"https://cdn.shopease.in/products/88/4e1c7b2a-....webp"}