Class: TpCommon::FileStorage::DirectUploaders::Public
- Defined in:
- lib/tp_common/file_storage/direct_uploaders/public.rb
Overview
Prepare a presigned url to upload diectly to storage service Use in case content to upload is outside of system/server,
we shoud use this to avoid delay because of content stream over our server
Constant Summary
Constants inherited from Base
Instance Method Summary collapse
-
#initialize(bucket_name = nil) ⇒ Public
constructor
A new instance of Public.
-
#presigned_post(file_key) ⇒ Object
Presigned POST url, use for form POST with data.
-
#presigned_put(file_key, mime_type, expires_in = 900) ⇒ Object
Presigned PUT url, use for PUT file to storage service.
-
#url(file_key) ⇒ Object
Public URL from key for uploaded file.
Constructor Details
#initialize(bucket_name = nil) ⇒ Public
Returns a new instance of Public.
9 10 11 12 |
# File 'lib/tp_common/file_storage/direct_uploaders/public.rb', line 9 def initialize(bucket_name = nil) @bucket_name = bucket_name || TpCommon::FileStorage.configuration.default_bucket @bucket = Aws::S3::Bucket.new(name: @bucket_name) end |
Instance Method Details
#presigned_post(file_key) ⇒ Object
Presigned POST url, use for form POST with data. Use when file name isn’t decided yet Request body is form data Return a hash
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tp_common/file_storage/direct_uploaders/public.rb', line 18 def presigned_post(file_key) s3_direct_post = @bucket.presigned_post(key: mask_key(file_key), success_action_status: '201', acl: 'public-read') { host: URI(s3_direct_post.url).host, bucket: @bucket_name, url: "#{s3_direct_post.url}/", form_data: s3_direct_post.fields } end |
#presigned_put(file_key, mime_type, expires_in = 900) ⇒ Object
Presigned PUT url, use for PUT file to storage service. File name must be matched, and request body is file content Return a hash
34 35 36 37 38 39 |
# File 'lib/tp_common/file_storage/direct_uploaders/public.rb', line 34 def presigned_put(file_key, mime_type, expires_in = 900) @bucket.object(mask_key(file_key)).presigned_url(:put, content_type: mime_type, acl: 'public-read', expires_in: expires_in) end |
#url(file_key) ⇒ Object
Public URL from key for uploaded file
43 44 45 |
# File 'lib/tp_common/file_storage/direct_uploaders/public.rb', line 43 def url(file_key) @bucket.object(mask_key(file_key))&.public_url(path_style: true) end |