The most common use case is saving an S3 object directly to your local disk. The get_object method accepts a :target parameter, which can be a file path or an IO object.
If you are processing a small file (like a CSV or JSON configuration) and don't need to save it to disk, you can load the content directly into a string variable. ruby download s3 file
: If running on EC2 or Lambda, use IAM Roles instead of hardcoded credentials. The most common use case is saving an
response = s3.get_object(bucket: bucket_name, key: 'config.json') file_content = response.body.read puts "File content: #file_content" Use code with caution. Method 3: Streaming Large Files : If running on EC2 or Lambda, use
: If you want to let a user download a private file directly from their browser, generate a presigned URL rather than proxying the download through your Ruby server.
💡 : Use the aws-sdk-s3 resource interface for a more object-oriented feel if you are managing complex bucket structures. If you'd like, I can help you with: Generating presigned URLs for browser downloads Setting up batch downloads for entire folders Configuring IAM policies for restricted access
Network issues or missing files can cause your script to fail. Always wrap your download logic in a begin-rescue block to handle common AWS errors.