Skip to content

Commit

Permalink
Updated S3 upload mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed Jul 27, 2021
1 parent 703fd95 commit 94482b6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 3 additions & 6 deletions spec/shrine/storage/s3_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ Spectator.describe Shrine::Storage::S3 do
context "without `prefix`" do
it "creates subdirectories" do
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/object?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})
.with(body: "").to_return(status: 200, body: "", headers: {"ETag" => "etag"})

expect(
subject.upload(FakeIO.new, "object")
Expand All @@ -62,8 +61,7 @@ Spectator.describe Shrine::Storage::S3 do

it "creates subdirectories" do
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/#{prefix}/a/a/a.jpg?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})
.with(body: "").to_return(status: 200, body: "", headers: {"ETag" => "etag"})

expect(
subject.upload(FakeIO.new, "a/a/a.jpg")
Expand All @@ -74,8 +72,7 @@ Spectator.describe Shrine::Storage::S3 do
context "with metadata" do
it "file uploads" do
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/a/a/a.jpg?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream", "Content-Disposition" => "inline; filename=\"ex\"; filename*=UTF-8''ex"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})
.with(body: "").to_return(status: 200, body: "", headers: {"ETag" => "etag"})

expect(
subject.upload(FakeIO.new, "a/a/a.jpg", metadata)
Expand Down
6 changes: 3 additions & 3 deletions src/shrine/storage/s3.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Shrine
getter bucket : String
getter client : Awscr::S3::Client
getter? public : Bool
getter uploader : Awscr::S3::FileUploader

# Initializes a storage for uploading to S3. All options are forwarded to
# [`Shrine::Storage::S3#initialize`], except the following:
Expand Down Expand Up @@ -38,7 +37,6 @@ class Shrine
@upload_options : Hash(String, String) = Hash(String, String).new,
@public : Bool = false
)
@uploader = Awscr::S3::FileUploader.new(client: client)
end

# Copies the file into the given location.
Expand All @@ -54,7 +52,9 @@ class Shrine
upload_options.each { |key, value| options[key.to_s] = value.to_s }

io = io.io if io.is_a?(UploadedFile)
uploader.upload(bucket, object_key(id), io, options)
client.put_object(bucket, object_key(id), io.gets_to_end, options)

true
end

def upload(io : IO | UploadedFile, id : String, metadata : Shrine::UploadedFile::MetadataType, move = false, **upload_options)
Expand Down

0 comments on commit 94482b6

Please sign in to comment.