Skip to content

Commit

Permalink
AWS: add option s3_acl_enabled (kreeti#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Sep 18, 2022
1 parent b020191 commit 705092a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def self.extended(base)
@s3_headers = {}
merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata)

@s3_acl_enabled = @options[:s3_acl_enabled]
@s3_acl_enabled = true if @s3_acl_enabled.nil?

@s3_storage_class = set_storage_class(@options[:s3_storage_class])

@s3_server_side_encryption = "AES256"
Expand Down Expand Up @@ -359,9 +362,12 @@ def flush_writes #:nodoc:
log("saving #{path(style)}")
write_options = {
content_type: file.content_type,
acl: s3_permissions(style)
}

if @s3_acl_enabled
write_options[:acl] = s3_permissions(style)
end

# add storage class for this style if defined
storage_class = s3_storage_class(style)
write_options.merge!(storage_class: storage_class) if storage_class
Expand Down
56 changes: 56 additions & 0 deletions spec/paperclip/storage/s3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,62 @@ def counter
end
end

context "An attachment that uses S3 for storage with acl disabled" do
before do
rebuild_model(
aws2_add_region.merge(
storage: :s3,
styles: { thumb: ["90x90#", :jpg] },
bucket: "bucket",
s3_acl_enabled: false,
s3_credentials: {
"access_key_id" => "12345",
"secret_access_key" => "54321"
}
)
)

@file = File.new(fixture_file("5k.png"), "rb")
@dummy = Dummy.new
@dummy.avatar = @file
@dummy.save
end

context "reprocess" do
before do
@object = double
allow(@dummy.avatar).to receive(:s3_object).with(:original).and_return(@object)
allow(@dummy.avatar).to receive(:s3_object).with(:thumb).and_return(@object)
allow(@object).to receive(:get).and_yield(@file.read)
allow(@object).to receive(:exists?).and_return(true)
allow(@object).to receive(:download_file).with(anything)
end

it "uploads original" do
expect(@object).to receive(:upload_file).with(
anything,
content_type: "image/png",
).and_return(true)
@dummy.avatar.reprocess!
expect(@object).to receive(:upload_file).with(
anything,
content_type: "image/png",
).and_return(true)
@dummy.avatar.reprocess!
end

it "doesn't upload original" do
expect(@object).to receive(:upload_file).with(
anything,
content_type: "image/png",
).and_return(true)
@dummy.avatar.reprocess!
end
end

after { @file.close }
end

context "An attachment that uses S3 for storage and has styles" do
before do
rebuild_model(
Expand Down

0 comments on commit 705092a

Please sign in to comment.