Problem with direct uploads in Rails 6 ActiveStorage

1.1k views Asked by At

I am getting the following erroor when trying to use the direct upload functionality of ActiveStorage. I have searched for the error unexpected value at params[:whitelist_headers], and the only results that I found suggested to upgrade to the latest version of aws-sdk-s3. However, I believe that I am using the latest version of s3. Here is my Gemfile:

# frozen_string_literal: true

source "https://rubygems.org"
ruby File.read(File.join(__dir__, ".ruby-version")).chomp

gem "rails", "~> 6.0"
gem "rails-i18n"

gem "bootsnap", require: false
gem "foreman"
gem "pg"
gem "puma", "~> 4.3"
...
...
...

gem 'aws-sdk-s3', require: false
gem 'mini_magick'

gem 'rack-cors

And the error message:

Started POST "/rails/active_storage/direct_uploads" for 127.0.0.1 at 2020-10-12 17:14:51 -0500
Processing by ActiveStorage::DirectUploadsController#create as JSON
  Parameters: {"blob"=>{"filename"=>"Screen Shot 2020-10-02 at 4.02.49 PM.png", "content_type"=>"image/png", "byte_size"=>120011, "checksum"=>"06QCMFP69Z4HT6w98ugkyQ=="}, "direct_upload"=>{"blob"=>{"filename"=>"Screen Shot 2020-10-02 at 4.02.49 PM.png", "content_type"=>"image/png", "byte_size"=>120011, "checksum"=>"06QCMFP69Z4HT6w98ugkyQ=="}}}
   (0.3ms)  BEGIN
  ActiveStorage::Blob Create (0.3ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["key", "ai7p1m5pknvo2wstwc6nw8ihf31h"], ["filename", "Screen Shot 2020-10-02 at 4.02.49 PM.png"], ["content_type", "image/png"], ["byte_size", 120011], ["checksum", "06QCMFP69Z4HT6w98ugkyQ=="], ["created_at", "2020-10-12 22:14:51.332510"]]
   (0.4ms)  COMMIT
  S3 Storage (0.8ms) Generated URL for file at key: ai7p1m5pknvo2wstwc6nw8ihf31h ()
Completed 500  in 10ms (ActiveRecord: 1.1ms | Allocations: 6659)

ArgumentError (unexpected value at params[:whitelist_headers]):

aws-sdk-core (3.46.2) lib/aws-sdk-core/param_validator.rb:32:in `validate!'
aws-sdk-core (3.46.2) lib/aws-sdk-core/param_validator.rb:13:in `validate!'
aws-sdk-core (3.46.2) lib/aws-sdk-core/plugins/param_validator.rb:23:in `call'
aws-sdk-core (3.46.2) lib/seahorse/client/plugins/raise_response_errors.rb:14:in `call'

Any help is appreciated

2

There are 2 answers

1
kales33 On

My problem was solved by replacing gem 'aws-sdk-s3' with gem 'aws-sdk' in my Gemfile.

This may not be an ideal solution, as the docs state the following:

Alternatively, the aws-sdk gem contains every available AWS service gem. This gem is very large; it is recommended to use it only as a quick way to migrate from V2 or if you depend on many AWS services.

0
F4-Z4 On

I run into similar issue with one older Ruby on Rails app. The issue in my case was that the aws-sdk-s3 gem introduced the whitelist_headers option in version 1.48.0 released on 30 Aug 2019. Rails 6 started using this option in version 6.0.3.1 released on 6 May 2020. Because Active Storage gem does not specify AWS SDK as its dependency, it’s quite possible you updated Active Storage while leaving AWS SDK behind (happened to me).