Class: Jammit::S3Uploader
- Inherits:
-
Object
- Object
- Jammit::S3Uploader
- Defined in:
- lib/jammit/s3_uploader.rb
Instance Method Summary collapse
- #find_or_create_bucket ⇒ Object
-
#initialize(options = {}) ⇒ S3Uploader
constructor
A new instance of S3Uploader.
- #log(msg) ⇒ Object
- #upload ⇒ Object
- #upload_from_glob(glob) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ S3Uploader
Returns a new instance of S3Uploader.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jammit/s3_uploader.rb', line 6 def initialize( = {}) @bucket = [:bucket] unless @bucket @bucket_name = [:bucket_name] || Jammit.configuration[:s3_bucket] @access_key_id = [:access_key_id] || Jammit.configuration[:s3_access_key_id] @secret_access_key = [:secret_access_key] || Jammit.configuration[:s3_secret_access_key] @bucket_location = [:bucket_location] || Jammit.configuration[:s3_bucket_location] @cache_control = [:cache_control] || Jammit.configuration[:s3_cache_control] @acl = [:acl] || Jammit.configuration[:s3_permission] @bucket = find_or_create_bucket end end |
Instance Method Details
#find_or_create_bucket ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jammit/s3_uploader.rb', line 87 def find_or_create_bucket s3_service = S3::Service.new(:access_key_id => @access_key_id, :secret_access_key => @secret_access_key) # find or create the bucket begin s3_service.buckets.find(@bucket_name) rescue S3::Error::NoSuchBucket log "Bucket not found. Creating '#{@bucket_name}'..." bucket = s3_service.buckets.build(@bucket_name) location = (@bucket_location.to_s.strip.downcase == "eu") ? :eu : :us bucket.save(location) bucket end end |
#log(msg) ⇒ Object
103 104 105 |
# File 'lib/jammit/s3_uploader.rb', line 103 def log(msg) puts msg end |
#upload ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jammit/s3_uploader.rb', line 20 def upload log "Pushing assets to S3 bucket: #{@bucket.name}" globs = [] # add default package path if Jammit.gzip_assets globs << "public/#{Jammit.package_path}/**/*.gz" else globs << "public/#{Jammit.package_path}/**/*.css" globs << "public/#{Jammit.package_path}/**/*.js" end # add images globs << "public/images/**/*" unless Jammit.configuration[:s3_upload_images] == false # add custom configuration if defined s3_upload_files = Jammit.configuration[:s3_upload_files] globs << s3_upload_files if s3_upload_files.is_a?(String) globs += s3_upload_files if s3_upload_files.is_a?(Array) # upload all the globs globs.each do |glob| upload_from_glob(glob) end end |
#upload_from_glob(glob) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jammit/s3_uploader.rb', line 46 def upload_from_glob(glob) log "Pushing files from #{glob}" log "#{ASSET_ROOT}/#{glob}" Dir["#{ASSET_ROOT}/#{glob}"].each do |local_path| next if File.directory?(local_path) remote_path = local_path.gsub(/^#{ASSET_ROOT}\/public\//, "") use_gzip = false # handle gzipped files if File.extname(remote_path) == ".gz" use_gzip = true remote_path = remote_path.gsub(/\.gz$/, "") end # check if the file already exists on s3 begin obj = @bucket.objects.find_first(remote_path) rescue obj = nil end # if the object does not exist, or if the MD5 Hash / etag of the # file has changed, upload it if !obj || (obj.etag != Digest::MD5.hexdigest(File.read(local_path))) log "pushing file to s3: #{remote_path}" # save to s3 new_object = @bucket.objects.build(remote_path) new_object.cache_control = @cache_control if @cache_control new_object.content_type = MimeMagic.by_path(remote_path) new_object.content = open(local_path) new_object.content_encoding = "gzip" if use_gzip new_object.acl = @acl if @acl new_object.save else log "file has not changed: #{remote_path}" end end end |