class TpCommon::FileStorage::DirectUploaders::Public

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

Public Class Methods

new(bucket_name = nil) click to toggle source
# 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

Public Instance Methods

presigned_post(file_key) click to toggle source

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

# 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) click to toggle source

Presigned PUT url, use for PUT file to storage service. File name must be matched, and request body is file content Return a hash

# 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) click to toggle source

Public URL from key for uploaded file

# 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