API リファレンスの upload_file
メソッドを参照してみると...
Returns:
(Boolean) — Returns true when the object is uploaded without any errors.
メソッドの返却値としては、(何事もなければ)必ず true
が返ってくるようですね
Raises:
(MultipartUploadError) — If an object is being uploaded in parts, and the upload can not be completed, then the upload is aborted and this error is raised. The raised error has a #errors method that returns the failures that caused the upload to be aborted.
処理に失敗した場合は 例外: Aws::S3::MultipartUploadError
が投げられるので、それを rescue
してあげるとよさそうですね
なので、イメージとしては以下のようなコードが考えられるかもしれません
ruby
1def test_bucket
2 Aws::S3::Resource.new(
3 :region => 'ap-northeast-1',
4 :access_key_id => '*************',
5 :secret_access_key => '*************',
6 ).bucket('test')
7end
8
9def upload_file(object_name, target_file)
10 # 正常終了すれば true が返る
11 test_bucket.object(object_name).upload_file(target_file)
12rescue Aws::S3::MultipartUploadError => _e
13 # (e.errors にStandardError オブジェクトの配列が格納されている)
14
15 # 失敗しても(リトライ処理など)何もしない
16 false
17end
18
19if upload_file('test.MP4', '****.MP4') # => true/false
20 puts 'アップロードに成功しました'
21else
22 puts 'アップロードに失敗しました'
23end
簡単に調べてみましたが、何か参考になれば幸いです
Links
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/25 04:39