How to download images from AWS S3 (rails, carrierwave)

environment ruby2.6.5 rails5.2.4 gem 'carrierwave' gem 'fog-aws'

Step 1 Let's stop Japanese file names

If the posted file name contains Japanese, subsequent processing will be difficult. With gem'carrierwave', you can rename the file as officially stated, so let's start from there. In my case, I decided to save the filename as a random alphanumeric character. Don't use the current time when saving. When resizing, the time will be off and an error will occur.

image_uploader.rb



  def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end

By writing in this way, the posted file name will be saved in random alphanumeric characters. Although the file name was changed with only filename, access to S3 did not work, but it was solved by adding def secure_token.

Step 2 Set the routing

routes.rb


  resources :posts do
    member do
      get :download
    end
  end

Step 3 Let's write the download method in the controller

posts_controller.rb



  def download
    url = URI.encode(@post.image.url)
    data_path = open(url)
    send_data data_path.read, disposition: 'attachment',
    type: @post.image_type
  end

Since my level is a beginner level, I think there is a waste in the description, but please forgive me.

Until the error and resolution encountered

When the file name first contained a Japanese name, there was nothing wrong with uploading. However, when I wrote the download method and tried to download it, 502 access denied was displayed. This 502 is awkward and at first I thought I didn't have access to AWS, but if I could upload it, I should have access. Further investigation reveals that 502 is displayed even if the URL is not found, Furthermore, it turned out that the cause was the Japanese notation. This is probably the reason why the files are often random alphanumeric files when downloaded on image posting sites.

Recommended Posts

How to download images from AWS S3 (rails, carrierwave)
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
How to save images on Heroku to S3 on AWS
[Rails] How to upload multiple images using Carrierwave
How to post images on Heroku + CarrierWave + S3
[rails] How to post images
[Note] Download from S3, upload to S3
[Rails] How to delete images uploaded by carrierwave (using devise)
[Ruby on Rails] How to use CarrierWave
[Rails] How to convert from erb to haml
How to link Rails6 Vue (from environment construction)
How to output CSV created by Rails to S3
How to write Rails
How to uninstall Rails
List how to learn from Docker to AKS on AWS
[AWS] How to check logs
How to handle uploaded images
Moving from AWS to PaizaCloud
[Rails] Save images using carrierwave
[Rails] How to use enum
[Rails] How to install devise
How to minimize Java images
[Rails] How to use enum
How to read rails routes
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
How to migrate from JUnit4 to JUnit5
[Rails] How to use Scope
How to write a migration from Rails datetime type to date type
[IOS] How to get the table name from AWS DynamoDB
[Rails] Create API to download files with Active Storage [S3]
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
How to deploy a Rails application on AWS (article summary)
[Rails] How to use gem "devise"
How to prevent past dates from being entered in Rails forms
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
How to push from Tarminal to GitHub
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
Save twitter icon to s3
[Note] Download from S3, upload to S3
upload images to refile heroku S3
upload images to refile heroku S3
Rails on Tiles (how to write)
[Rails] How to write exception handling?