Class: InovaAwsS3::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/inova_aws_s3/service.rb

Constant Summary collapse

PART_SIZE =
1024 * 1024 * 10

Instance Method Summary collapse

Constructor Details

#initialize(acl: InovaAwsS3.configuration.acl) ⇒ Service

Returns a new instance of Service.



6
7
8
9
10
11
12
13
14
15
# File 'lib/inova_aws_s3/service.rb', line 6

def initialize(acl: InovaAwsS3.configuration.acl)
  @acl = acl
  @bucket_name = InovaAwsS3.configuration.bucket_name
  @region = InovaAwsS3.configuration.region
  @access_key = InovaAwsS3.configuration.access_key
  @secret_key = InovaAwsS3.configuration.secret_key
  @folder_name = InovaAwsS3.configuration.folder_name
  set_s3_client
  set_s3_resource
end

Instance Method Details

#change_acl(url: nil, new_acl: 'authenticated-read') ⇒ Object



73
74
75
76
77
78
79
# File 'lib/inova_aws_s3/service.rb', line 73

def change_acl(url: nil, new_acl: 'authenticated-read')
  return if url.nil?
  key = get_key_from_url(url)
  @s3_client.put_object_acl({ acl: new_acl, bucket: @bucket_name, key: key })
rescue Exception
  puts "error in #{url}"
end

#delete_file(url) ⇒ Object



112
113
114
115
116
# File 'lib/inova_aws_s3/service.rb', line 112

def delete_file(url)
  key = get_key_from_url(url)
  return if key.blank?
  @s3_client.delete_object({ bucket: @bucket_name, key: key })
end

#generate_get_presigned(url) ⇒ Object



99
100
101
102
103
104
# File 'lib/inova_aws_s3/service.rb', line 99

def generate_get_presigned(url)
  key = get_key_from_url(url)
  object = @s3_resource.bucket(@bucket_name).object(key)
  presigned_url = object.presigned_url(:get, expires_in: 10800) # 3 hours
  presigned_url
end

#get_key(filename) ⇒ Object



94
95
96
97
# File 'lib/inova_aws_s3/service.rb', line 94

def get_key(filename)
  key = @folder_name + filename
  return key
end

#get_key_from_url(url) ⇒ Object



81
82
83
84
85
86
# File 'lib/inova_aws_s3/service.rb', line 81

def get_key_from_url(url)
  # uri = URI.parse(url)
  # File.basename(uri.path)
  arr = url.split("/")
  arr[3..-1]&.join("/")
end

#get_presigned_url(file_name) ⇒ Object



106
107
108
109
110
# File 'lib/inova_aws_s3/service.rb', line 106

def get_presigned_url(file_name)
  object = @s3_resource.bucket(@bucket_name).object(@folder_name.to_s + file_name)
  presigned_url = object.presigned_url(:put, { acl: "public-read" }) #, expires: 10*60)
  { presigned_url: presigned_url, file_url: object.public_url }
end

#multipart_upload(filename: nil, file: nil, acl: "public-read") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inova_aws_s3/service.rb', line 25

def multipart_upload(filename: nil, file: nil, acl: "public-read")
  logger ||= Logger.new("#{Rails.root}/log/uploads.log")
  key = @folder_name + filename
  resp = @s3_client.create_multipart_upload({
                                              bucket: @bucket_name,
                                              key: key, acl: acl })
  current_part = 1
  parts = []
  file = File.open(file, 'rb')
  index = 1
  until file.eof?
    logger.info("#{key} Part #{current_part}")
    part = file.read(PART_SIZE)
    resp_upload = @s3_client.upload_part({
                                           body: part,
                                           bucket: @bucket_name,
                                           key: key,
                                           part_number: current_part,
                                           upload_id: resp.upload_id,
                                         })

    parts << { etag: resp_upload.etag, part_number: current_part }
    current_part = current_part + 1
  end
  resp_complete = @s3_client.complete_multipart_upload({
                                                         bucket: @bucket_name, # required
                                                         key: key, # required
                                                         multipart_upload: {
                                                           parts: parts
                                                         },
                                                         upload_id: resp.upload_id # required
                                                       })
  file.close()
  set_object(key).public_url
end

#onepart_upload(file_name: nil, tempfile: nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/inova_aws_s3/service.rb', line 61

def onepart_upload(file_name: nil, tempfile: nil)
  file_name = "#{SecureRandom.urlsafe_base64}_#{Time.now.strftime('%s')}_#{File.extname(file_name)}".gsub(/\s+/, "")
  key = @folder_name + file_name
  obj = set_object(key)
  obj.upload_file(tempfile, { acl: @acl })
  obj.public_url
end

#set_object(key) ⇒ Object



69
70
71
# File 'lib/inova_aws_s3/service.rb', line 69

def set_object(key)
  @s3_resource.bucket(@bucket_name).object(key)
end

#set_s3_clientObject



17
18
19
# File 'lib/inova_aws_s3/service.rb', line 17

def set_s3_client
  @s3_client = Aws::S3::Client.new(region: @region, access_key_id: @access_key, secret_access_key: @secret_key)
end

#set_s3_resourceObject



21
22
23
# File 'lib/inova_aws_s3/service.rb', line 21

def set_s3_resource
  @s3_resource = Aws::S3::Resource.new(region: @region, access_key_id: @access_key, secret_access_key: @secret_key)
end

#valid_s3_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/inova_aws_s3/service.rb', line 88

def valid_s3_url?(url)
  arr = url.split("/")
  host_name = "#{@bucket_name}.s3.#{@region}.amazonaws.com"
  return arr[2] == host_name
end