Module: Worochi::Helper
- Defined in:
- lib/worochi/helper.rb
Overview
Contains any global helper methods not specific to any individual service.
Class Method Summary collapse
-
.is_s3_path?(path) ⇒ Boolean
Check if a given path is an S3 path.
-
.s3_url(path) ⇒ URI::HTTPS
Given an S3 path, return the full URL for the corresponding object determined using the AWS SDK.
Class Method Details
.is_s3_path?(path) ⇒ Boolean
Check if a given path is an S3 path.
38 39 40 |
# File 'lib/worochi/helper.rb', line 38 def is_s3_path?(path) !s3_prefix_re.match(path).nil? end |
.s3_url(path) ⇒ URI::HTTPS
Given an S3 path, return the full URL for the corresponding object determined using the AWS SDK. AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID should be present in ENV. The string should be formatted as: ‘s3:(bucket_name:)path/to/file`.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/worochi/helper.rb', line 22 def s3_url(path) raise Error, 'Invalid S3 path' unless is_s3_path?(path) path = path.sub(s3_prefix_re, '') if match = /^(.*)\:/.match(path) bucket = match[1] path = path.sub(/^(.*)\:/, '') end bucket ||= Config.s3_bucket raise Error, 'S3 bucket name is not defined' if bucket.nil? AWS::S3.new.buckets[bucket].objects[path].url_for(:read) end |