Skip to content

Commit

Permalink
Allow encoding to be specified with S3 storage class (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
pond authored Oct 6, 2022
1 parent 606b807 commit 59cdf1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/shrine/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,19 @@ def upload(io, id, shrine_metadata: {}, **upload_options)
# Returns a `Down::ChunkedIO` object that downloads S3 object content
# on-demand. By default, read content will be cached onto disk so that
# it can be rewinded, but if you don't need that you can pass
# `rewindable: false`.
# `rewindable: false`. A required character encoding can be passed in
# `encoding`; the default is `Encoding::BINARY` via `Down::ChunkedIO`.
#
# Any additional options are forwarded to [`Aws::S3::Object#get`].
#
# [`Aws::S3::Object#get`]: http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#get-instance_method
def open(id, rewindable: true, **options)
def open(id, rewindable: true, encoding: nil, **options)
chunks, length = get(id, **options)

Down::ChunkedIO.new(chunks: chunks, rewindable: rewindable, size: length)
down_options = {chunks: chunks, rewindable: rewindable, size: length}
down_options[:encoding] = encoding unless encoding.nil?

Down::ChunkedIO.new(**down_options)
rescue Aws::S3::Errors::NoSuchKey
raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end
Expand Down
6 changes: 6 additions & 0 deletions test/storage/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ def encryption_client
assert_raises(IOError) { io.rewind }
end

it "accepts :encoding option" do
@s3.client.stub_responses(:get_object, status_code: 200, headers: { "content-length" => "7" }, body: "content")
io = @s3.open("foo", encoding: 'UTF-8')
assert_equal Encoding::UTF_8, io.read().encoding
end

it "returns Shrine::FileNotFound when object was not found" do
@s3.client.stub_responses(:get_object, "NoSuchKey")
assert_raises(Shrine::FileNotFound) { @s3.open("nonexisting") }
Expand Down

0 comments on commit 59cdf1f

Please sign in to comment.