diff options
author | Cameron Bothner <cameronbothner@gmail.com> | 2018-08-18 13:31:33 -0400 |
---|---|---|
committer | Cameron Bothner <cameronbothner@gmail.com> | 2018-08-21 15:31:14 -0400 |
commit | 5cd2d07bdcda4b2f547830d2becafe5e0722fa43 (patch) | |
tree | 78cd529618c2032f1f9049ae2b1c052eaaf23800 /activestorage/lib/active_storage/service/s3_service.rb | |
parent | 87d5415f0aa3e6f9f74f645a47370dd854375a1a (diff) | |
download | rails-5cd2d07bdcda4b2f547830d2becafe5e0722fa43.tar.gz rails-5cd2d07bdcda4b2f547830d2becafe5e0722fa43.tar.bz2 rails-5cd2d07bdcda4b2f547830d2becafe5e0722fa43.zip |
Translate service-specific missing object exceptions into a generic one
`ActiveStorage::Blob#download` and `ActiveStorage::Blob#open` raise
`ActiveStorage::FileNotFoundError` when the corresponding file is missing
from the storage service. Services translate service-specific missing
object exceptions (e.g. `Google::Cloud::NotFoundError` for the GCS service
and `Errno::ENOENT` for the disk service) into
`ActiveStorage::FileNotFoundError`.
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 |