Class: AWSUploader
- Defined in:
- lib/mrpin/core/uploaders/aws/aws_uploader.rb
Constant Summary collapse
- BUCKET_NAME_DEFAULT =
'mrpin-static'
- DOMAIN_NAME_CLOUD_FRONT =
'd3crzre36asem1.cloudfront.net'
- DOMAIN_NAME_S3 =
's3.amazonaws.com'
- AWS_REGION =
'us-east-1'
- CACHE_CONTROL_MAX_AGE_DEFAULT =
365 * 24 * 60 * 60
Instance Attribute Summary collapse
-
#cloud_front ⇒ Object
readonly
Returns the value of attribute cloud_front.
-
#s3 ⇒ Object
readonly
Returns the value of attribute s3.
Instance Method Summary collapse
- #check_s3_object_exists(url, bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
- #delete_file_from_s3(bucket_name, key) ⇒ Object
- #delete_file_from_s3_by_uri(bucket_name, url) ⇒ Object
- #get_cloud_front_url(url, use_ssl = true) ⇒ Object
- #get_distribution_id_by(bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
- #get_s3_url(url, bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
-
#initialize(options = {}) ⇒ AWSUploader
constructor
A new instance of AWSUploader.
- #invalidate_distribution(distribution_id, keys) ⇒ Object
- #upload_data_to_s3(data_to_upload, bucket_name, path, desired_name = nil) ⇒ Object
- #upload_file_to_s3(file_to_upload, bucket_name, key) ⇒ Object
- #upload_files_to_s3(files_to_upload, bucket_name, keys) ⇒ Object
- #upload_url_file_to_s3(url_to_upload, bucket_name, key) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AWSUploader
Returns a new instance of AWSUploader.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 15 def initialize( = {}) credentials = {} credentials[:access_key_id] = get_var_from_env('AWS_ACCESS_KEY_ID') credentials[:secret_access_key] = get_var_from_env('AWS_SECRET_ACCESS_KEY') credentials[:region] = AWS_REGION @s3 = Aws::S3::Client.new(credentials) @s3_resources = Aws::S3::Resource.new(credentials) @cloud_front = Aws::CloudFront::Client.new(credentials) end |
Instance Attribute Details
#cloud_front ⇒ Object (readonly)
Returns the value of attribute cloud_front.
13 14 15 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 13 def cloud_front @cloud_front end |
#s3 ⇒ Object (readonly)
Returns the value of attribute s3.
12 13 14 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 12 def s3 @s3 end |
Instance Method Details
#check_s3_object_exists(url, bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 213 def check_s3_object_exists(url, bucket_name = BUCKET_NAME_DEFAULT) key = get_key_from_url(url, bucket_name) s3_object = @s3_resources.bucket(bucket_name).object(key) s3_object.exists? end |
#delete_file_from_s3(bucket_name, key) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 124 def delete_file_from_s3(bucket_name, key) bucket_name ||= BUCKET_NAME_DEFAULT return unless check_s3_object_exists(key, bucket_name) s3_object = @s3_resources.bucket(bucket_name).object(key) s3_object.delete nil end |
#delete_file_from_s3_by_uri(bucket_name, url) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 114 def delete_file_from_s3_by_uri(bucket_name, url) bucket_name ||= BUCKET_NAME_DEFAULT key = get_key_from_url(url, bucket_name) delete_file_from_s3(bucket_name, key) end |
#get_cloud_front_url(url, use_ssl = true) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 142 def get_cloud_front_url(url, use_ssl = true) result = url.sub URI.parse(url).host, DOMAIN_NAME_CLOUD_FRONT if use_ssl result.sub! 'http:/', 'https:/' else result.sub! 'https:/', 'http:/' end result end |
#get_distribution_id_by(bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 156 def get_distribution_id_by(bucket_name = BUCKET_NAME_DEFAULT) result = nil response = @cloud_front.list_distributions list_distributions = response[:items] list_distributions.each do |distribution| origin_settings = distribution[:origins] origin_item_first = origin_settings[:items].first origin_domain_name = origin_item_first[:domain_name] origin_name = origin_domain_name.split('.s3.').first if origin_name == bucket_name result = distribution[:id] break end end result end |
#get_s3_url(url, bucket_name = BUCKET_NAME_DEFAULT) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 204 def get_s3_url(url, bucket_name = BUCKET_NAME_DEFAULT) result = url.sub URI.parse(url).host, "#{bucket_name}.#{DOMAIN_NAME_S3}" result.sub! 'http:/', 'https:/' result end |
#invalidate_distribution(distribution_id, keys) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 179 def invalidate_distribution(distribution_id, keys) assert(!distribution_id.nil?, 'aws:invalidate_distribution. distribution_id must be non nil') assert(keys.is_a?(Array), 'aws:invalidate_distribution. distribution_id must be non nil') keys = keys.map do |k| k.start_with?('/') ? k : '/' + k end paths = {} paths[:items] = keys paths[:quantity] = keys.size invalidation_batch = {} invalidation_batch[:caller_reference] = Time.now.to_i.to_s invalidation_batch[:paths] = paths = {} [:distribution_id] = distribution_id [:invalidation_batch] = invalidation_batch response = @cloud_front.create_invalidation() end |
#upload_data_to_s3(data_to_upload, bucket_name, path, desired_name = nil) ⇒ Object
73 74 75 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 73 def upload_data_to_s3(data_to_upload, bucket_name, path, desired_name = nil) upload_to_s3_internal(data_to_upload, bucket_name, path, desired_name) end |
#upload_file_to_s3(file_to_upload, bucket_name, key) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 61 def upload_file_to_s3(file_to_upload, bucket_name, key) result = nil File.open(file_to_upload) do |data_to_upload| result = upload_to_s3_internal(data_to_upload, bucket_name, key, file_to_upload) end result end |
#upload_files_to_s3(files_to_upload, bucket_name, keys) ⇒ Object
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 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 29 def upload_files_to_s3(files_to_upload, bucket_name, keys) bucket_name ||= BUCKET_NAME_DEFAULT assert(!files_to_upload.nil?, 'aws:upload_files_to_s3. Files to upload must be non nil') assert(!bucket_name.nil?, 'aws:upload_files_to_s3. Bucket name must be non nil') assert(!keys.nil? && keys.size == files_to_upload.size, 'aws:upload_files_to_s3. Desired names must be non nil and its count must be equal count of files to upload') result = [] begin [0..files_to_upload.size - 1].each do |i| file_to_upload = files_to_upload[i] s3_key = keys[i] result << upload_file_to_s3(file_to_upload, bucket_name, s3_key) end rescue Exception => e result.each do |file_info| delete_file_from_s3_by_uri(bucket_name, file_info.url_s3) end assert(false, e.to_s) end result end |
#upload_url_file_to_s3(url_to_upload, bucket_name, key) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mrpin/core/uploaders/aws/aws_uploader.rb', line 78 def upload_url_file_to_s3(url_to_upload, bucket_name, key) result = nil open(url_to_upload) do |data_to_upload| result = upload_to_s3_internal(data_to_upload, bucket_name, key, url_to_upload) end result.source = url_to_upload result end |