ActiveStorage for file uploads

6700
0

ActiveStorage provides a unified interface for uploading files to cloud storage services like S3, GCS, or Azure Storage. I configure storage services in config/storage.yml and attach files to models using has_one_attached or has_many_attached macros. Files are stored with metadata in the database while blobs live in the configured storage service. Variants support on-the-fly image transformations using ImageProcessing gem. Direct uploads from browser to S3 bypass the Rails app entirely, improving performance and reducing server load. For APIs, I generate presigned URLs for clients to upload directly, then create the ActiveStorage attachment after successful upload. Proper security includes validating content types and file sizes before processing.