Skip to content

Commit

Permalink
Add relevant methods to use :aws as a cache storage.
Browse files Browse the repository at this point in the history
See carrierwaveuploader/carrierwave#1312 for more information.
  • Loading branch information
fschwahn committed Feb 23, 2017
1 parent d616ba4 commit 182d267
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/carrierwave/storage/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ def retrieve!(identifier)
AWSFile.new(uploader, connection, uploader.store_path(identifier))
end

def cache!(file)
AWSFile.new(uploader, connection, uploader.cache_path).tap do |aws_file|
aws_file.store(file)
end
end

def retrieve_from_cache!(identifier)
AWSFile.new(uploader, connection, uploader.cache_path(identifier))
end

def delete_dir!(path)
# NOTE: noop, because there are no directories on S3
end

def clean_cache!(_seconds)
raise 'use Object Lifecycle Management to clean the cache'
end

def connection
@connection ||= begin
conn_cache = self.class.connection_cache
Expand Down
13 changes: 12 additions & 1 deletion lib/carrierwave/storage/aws_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def read
end

def store(new_file)
file.put(aws_options.write_options(new_file))
if new_file.is_a?(self.class)
new_file.move_to(path)
else
file.put(aws_options.write_options(new_file))
end
end

def copy_to(new_path)
Expand All @@ -57,6 +61,13 @@ def copy_to(new_path)
)
end

def move_to(new_path)
file.move_to(
"#{bucket.name}/#{new_path}",
acl: uploader.aws_acl
)
end

def signed_url(options = {})
signer.call(public_url.dup, options)
end
Expand Down
29 changes: 29 additions & 0 deletions spec/features/moving_files_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

describe 'Moving Files', type: :feature do
let(:image) { File.open('spec/fixtures/image.png', 'r') }
let(:original) { FeatureUploader.new }

it 'copies an existing file to the specified path' do
original.store!(image)
original.retrieve_from_store!('image.png')

original_attributes = original.file.attributes
original_acl_grants = original.file.file.acl.grants

original.file.move_to('uploads/image2.png')

move = FeatureUploader.new
move.retrieve_from_store!('image2.png')

copy_attributes = move.file.attributes
copy_acl_grants = move.file.file.acl.grants

expect(copy_attributes).to eq(original_attributes)
expect(copy_acl_grants).to eq(original_acl_grants)
expect(original.file).not_to exist

image.close
move.file.delete
end
end
28 changes: 28 additions & 0 deletions spec/features/storing_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,32 @@
image.close
instance.file.delete
end

it 'uploads the cache file to the configured bucket' do
instance.cache!(image)
instance.retrieve_from_cache!(instance.cache_name)

expect(instance.file.size).to be_nonzero
expect(image.size).to eq(instance.file.size)

image.close
instance.file.delete
end

it 'moves cached files to final location when storing' do
instance.cache!(image)
cache_name = instance.cache_name
instance.store!

instance.retrieve_from_cache!(cache_name)
expect(instance.file).not_to exist

instance.retrieve_from_store!('image.png')
expect(instance.file.size).to eq(image.size)
expect(instance.file.read).to eq(image.read)
expect(instance.file.read).to eq(instance.file.read)

image.close
instance.file.delete
end
end
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ def filename

config.before(:all, type: :feature) do
CarrierWave.configure do |cw_config|
cw_config.storage = :aws
cw_config.aws_bucket = ENV['S3_BUCKET_NAME']
cw_config.aws_acl = :'public-read'
cw_config.storage = :aws
cw_config.cache_storage = :aws
cw_config.aws_bucket = ENV['S3_BUCKET_NAME']
cw_config.aws_acl = :'public-read'

cw_config.aws_credentials = {
access_key_id: ENV['S3_ACCESS_KEY'],
Expand Down

0 comments on commit 182d267

Please sign in to comment.