diff options
author | George Claghorn <george.claghorn@gmail.com> | 2018-08-22 22:56:10 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-22 22:56:10 -0400 |
commit | dc001dbd58cde8b2caab42ab1bcdd88774818c53 (patch) | |
tree | 4704d8e8959bcd6569cc4244ec131e7dbedf87bc /activestorage/lib/active_storage/service/s3_service.rb | |
parent | cdee52079cdd88d376d9664a61a40348d45e819c (diff) | |
parent | 22efb2ec49087827ca1cb28a8bad9f016800c591 (diff) | |
download | rails-dc001dbd58cde8b2caab42ab1bcdd88774818c53.tar.gz rails-dc001dbd58cde8b2caab42ab1bcdd88774818c53.tar.bz2 rails-dc001dbd58cde8b2caab42ab1bcdd88774818c53.zip |
Merge pull request #33666 from cbothner/fail-gracefully-from-activestorage-file-not-found
Fail more gracefully from ActiveStorage missing file exceptions
Diffstat (limited to 'activestorage/lib/active_storage/service/s3_service.rb')
-rw-r--r-- | activestorage/lib/active_storage/service/s3_service.rb | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/service/s3_service.rb b/activestorage/lib/active_storage/service/s3_service.rb index 0286e7ff21..89a9e54158 100644 --- a/activestorage/lib/active_storage/service/s3_service.rb +++ b/activestorage/lib/active_storage/service/s3_service.rb @@ -33,14 +33,22 @@ module ActiveStorage end else instrument :download, key: key do - object_for(key).get.body.string.force_encoding(Encoding::BINARY) + begin + object_for(key).get.body.string.force_encoding(Encoding::BINARY) + rescue Aws::S3::Errors::NoSuchKey + raise ActiveStorage::FileNotFoundError + end end end end def download_chunk(key, range) instrument :download_chunk, key: key, range: range do - object_for(key).get(range: "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}").body.read.force_encoding(Encoding::BINARY) + begin + object_for(key).get(range: "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}").body.read.force_encoding(Encoding::BINARY) + rescue Aws::S3::Errors::NoSuchKey + raise ActiveStorage::FileNotFoundError + end end end @@ -103,6 +111,8 @@ module ActiveStorage chunk_size = 5.megabytes offset = 0 + raise ActiveStorage::FileNotFoundError unless object.exists? + while offset < object.content_length yield object.get(range: "bytes=#{offset}-#{offset + chunk_size - 1}").body.read.force_encoding(Encoding::BINARY) offset += chunk_size |