Class: Uploader::S3Service
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Service
#initialize, #initialized?, is_registered?, register, services
Class Method Details
.content_types ⇒ Object
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
54
55
56
|
# File 'lib/uploader/s3_service.rb', line 54
def close!
AWS::S3::Base.disconnect!
end
|
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!
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!
object = @s3_bucket.new_object
object.key = destination
object.value = contents
object.store
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
|