[Rails] Create API to download files with Active Storage [S3]

things to do

If you use Rails on the backend, you can hit the URL to download the file.

Premise

I'm assuming you're uploading a file with Active Storage. ** Save destination (using S3 or minio) **

Implementation

routing

This time, set the routing so that it is **/upload_files /: id/download **.

resources :upload_files do
  member do
    get :download
  end
end

controller

 def download
    upload_file = UploadFile.find(params[:id])

    file = upload_file.file.blob.download  #to download

    if send_data(file, disposition: 'attachment',  #Send the downloaded file
      filename: upload_file.file.blob.filename.to_s, #Get file name
      type: upload_file.file.blob.content_type) # content_Get type
      head :no_content #If you can send, no_Returns content
    else
      render json: upload_file.errors, status: :not_found  #Returns an error
    end
  end

You can actually download the file from Active Storage by setting file = upload_file.file.blob.download.

I was having a hard time finding the above method, but it was written in the official ActiveStorage documentation! ** ** If you are in trouble, the formula is the best. https://railsguides.jp/active_storage_overview.html

In addition, the file name is dynamically acquired by setting upload_file.file.blob.filename.to_s. This will cause the ** filename to correctly include the extension. ** **

Impressions

I realized that it is important to read the formula if you are in trouble!

Recommended Posts

[Rails] Create API to download files with Active Storage [S3]
[Rails 6] Add images to seed files (using Active Storage)
[Rails] How to use Active Storage
[Rails] rails new to create a database with PostgreSQL
How to build API with GraphQL and Rails
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
[Rails 6] RuntimeError with $ rails s
[Ruby on Rails] Delete s3 images with Active Strage
Procedure to use S3 of LocalStack for Active Storage
How to download images from AWS S3 (rails, carrierwave)
Tutorial to create a blog with Rails for beginners Part 1
[Rails API + Vue] Upload and display images using Active Storage
[Rails] I tried to create a mini app with FullCalendar
API creation with Rails + GraphQL
A series of steps to create portfolio deliverables with Rails
Tutorial to create a blog with Rails for beginners Part 2
Post videos with Active Storage
Tutorial to create a blog with Rails for beginners Part 0
I want to convert an array to Active Record Relation with Rails
[rails6.0.0] How to save images using Active Storage in wizard format
Preparing to create a Rails application
Move Active Storage on S3 locally
Create pagination function with Rails Kaminari
Download large files with Apache JMeter
Connect to Rails server with iPhone
How to return Rails API mode to Rails
How to get along with Rails
Create My Page with Rails devise
Introducing React to Rails with react-rails
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
How to share files with Docker Toolbox
[Rails6] Create a new app with Rails [Beginner]
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
Upload Rails app image file to S3
[Rails withdrawal] Create a simple withdrawal function with rails
* Android * Saving / loading files to internal storage
[Active Storage] Validation settings when uploading files
How to achieve file download with Feign
[Rails] Let's create a super simple Rails API
Rails Active Storage shrinks images before uploading
[rails] How to create a partial template
[Rails 5] Create a new app with Rails [Beginner]
[Rails] Book search with Amazon PA API
Post / delete multiple images with Active Storage
How to build Rails 6 environment with Docker
Dealing with NameError: uninitialized constant :: Analyzable error when installing Active Storage in Rails6
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
How to implement image posting function using Active Storage in Ruby on Rails
Problem of slow processing when using Active Storage with cloud storage (GCS, S3, etc)
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6