Skip to content

Commit

Permalink
Prevent timeout errors when storing files
Browse files Browse the repository at this point in the history
According to aws-ruby's maintainer upload timeouts typically occur
because the content length couldn't be determined directly. The fix is
fairly simple, just pass a path to the file instead of the file data and
the gem will open it in binary mode itself.

An integration test (and all of the support around that) had to be put
in place to maintain confidence about the test.
  • Loading branch information
sorentwo committed May 23, 2013
1 parent cfa2186 commit 291fc1b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .env.sample
@@ -0,0 +1,3 @@
S3_BUCKET_NAME=BUCKET_NAME
S3_ACCESS_KEY=YOUR_KEY
S3_SECRET_ACCESS_KEY=YOUR_KEY
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,6 +1,8 @@
*.gem
.bundle
.config
.env
Gemfile.lock
bin
vendor
uploads
6 changes: 1 addition & 5 deletions lib/carrierwave/storage/aws.rb
Expand Up @@ -73,15 +73,11 @@ def size
end

def store(new_file)
aws_file = new_file.to_file

@file = bucket.objects[path].write(aws_file, {
@file = bucket.objects[path].write(new_file.file, {
acl: uploader.aws_acl,
content_type: new_file.content_type
}.merge(uploader.aws_attributes || {}))

aws_file.close unless aws_file.closed?

true
end

Expand Down
29 changes: 29 additions & 0 deletions spec/features/storing_files_spec.rb
@@ -0,0 +1,29 @@
require 'spec_helper'

if ENV['S3_BUCKET_NAME']
describe 'Storing Files' do
before(:all) do
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = ENV['S3_BUCKET_NAME']
config.aws_acl = :public_read

config.aws_credentials = {
access_key_id: ENV['S3_ACCESS_KEY'],
secret_access_key: ENV['S3_SECRET_ACCESS_KEY']
}
end
end

it 'uploads the file to the configured bucket' do
image = File.open('spec/fixtures/image.png', 'r')
uploader = Class.new(CarrierWave::Uploader::Base)
instance = uploader.new

expect {
instance.store!(image)
instance.retrieve_from_store!('image_file.png')
}.to_not raise_error
end
end
end
Binary file added spec/fixtures/image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,9 +2,20 @@
require 'carrierwave'
require 'carrierwave-aws'

def source_environment_file!
return unless File.exists?('.env')

File.readlines('.env').each do |line|
values = line.split('=')
ENV[values.first] = values.last.chomp
end
end

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus
config.order = 'random'
config.run_all_when_everything_filtered = true

source_environment_file!
end

0 comments on commit 291fc1b

Please sign in to comment.