Class: Uploader::S3Service

Inherits:
Service show all
Defined in:
lib/uploader/s3_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#initialize, #initialized?, is_registered?, register, services

Constructor Details

This class inherits a constructor from Uploader::Service

Class Method Details

.content_typesObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/uploader/s3_service.rb', line 118

def self.content_types
  {
    ".png" => "image/png",
    ".jpg" => "image/jpeg",
    ".jpeg" => "image/jpeg",
    ".pdf" => "application/pdf",
    ".jar" => "application/java-archive",
    ".txt" => "text/plain",
    ".html" => "text/html",
    ".css" => "text/css",
    ".js" => "text/javascript"
  }
end

Instance Method Details

#close!Object



54
55
56
# File 'lib/uploader/s3_service.rb', line 54

def close!
  AWS::S3::Base.disconnect!
end

#connect!Object



9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
# File 'lib/uploader/s3_service.rb', line 9

def connect!
  # TODO S3 may not work yet.

  if not SystemChecks.gem_available?('aws/s3')
    $stderr.puts %Q{\nThe Amazon S3 service requires the aws-s3 gem (vers >= 0.6.2), which is 
not required by default. Run 'gem install aws-s3' to install it.\n\n}
    raise LoadError
  end

  @bucket_name = @config.bucket
  access_key = ENV[@config.access_key]
  secret_key = ENV[@config.secret_key]

  $stderr.puts "Connecting to AWS..."

  @suppressor.shutup!

  unless AWS::S3::Base.connected?
    options = {
        :access_key_id => access_key,
        :secret_access_key => secret_key,
        :use_ssl => true,
        :port => 443
      }

    if @config.use_proxy
      options.update({
        :proxy => {
          :host => @config.proxy['host'],
          :port => @config.proxy['port']
          }
        })
    end

    AWS::S3::Base.establish_connection!(options)
    @suppressor.ok

  else
    @suppressor.ok
    $stderr.puts "AWS already connected."
  end

  @s3_bucket = AWS::S3::Bucket.find(@bucket_name)
end

#upload(remote_path, contents) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/uploader/s3_service.rb', line 58

def upload(remote_path, contents)
  raise UploadArgumentError if not remote_path.is_a?(Path)

  if contents.is_a?(File)
    contents_to_upload = contents.read

  elsif contents.is_a?(String)
    contents = contents

  else
    raise UnrecognizedContentType

  end

  upload_contents_to_s3(local_path, contents_to_upload)
end

#upload_contents_to_s3(remote_path, contents, mime_type = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/uploader/s3_service.rb', line 75

def upload_contents_to_s3(remote_path, contents, mime_type=nil)
  $stderr.puts "Attempting to upload #{remote_path} to S3"

  destination = Path.new(@config.remote_root_path) + remote_path

  @suppressor.shutup!

  # Prepare the object.
  object = @s3_bucket.new_object
  object.key = destination
  object.value = contents

  # Store the object first...
  object.store

  #if not mime_type.blank?
  #  #object.content_type = mime_type
  #  #object.store
  #else
  #  ext = File.extname(local_path)
  #  if content_types.keys.include? ext
  #    #object.content_type = content_types[ext]
  #    #object.store
  #  end
  #end

  # ... then set the ACLs.
  public_read_grant = AWS::S3::ACL::Grant.grant(:public_read)
  object.acl.grants << public_read_grant
  object.acl(object.acl)

  @suppressor.ok

  if @s3_bucket.objects.include?(object)
    $stderr.puts "Successfully uploaded #{local_path} to S3!"
  else
    $stderr.puts "Something went wrong uploading #{local_path} to S3."
  end
rescue => detail
  $stderr.puts "Uploader#upload_contents_to_s3 exception."
  $stderr.puts detail.message
end