forked from deis/example-ruby-sinatra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.rb
More file actions
70 lines (53 loc) · 1.72 KB
/
web.rb
File metadata and controls
70 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'sinatra'
require 'aws/s3'
set :port, ENV["PORT"] || 5000
get '/' do
whom = ENV["POWERED_BY"] || "Deis!"
container = `hostname`.strip || "unknown"
AWS::S3::Base.establish_connection!(
server: ENV['S3_HOST'],
port: ENV['S3_PORT'],
access_key_id: ENV['S3_ACCESS_KEY_ID'],
secret_access_key: ENV['S3_SECRET_ACCESS_KEY']
)
bucket_list = {}
AWS::S3::Service.buckets.each do |bucket|
bucket_content = []
p bucket
AWS::S3::Bucket.find(bucket.name).each do |object|
bucket_content.push "#{object.key}\t#{object.about['content-length']}\t#{object.about['last-modified']}"
p " #{object.to_s}"
end
p '_____________'
bucket_list[bucket.name] = bucket_content
end
bucket_name = ENV['S3_BUCKET_NAME'] || 'deis-store.local3.deisapp.com/fancy_new_bucket'
AWS::S3::Bucket.find(bucket_name).each do |object|
bucket_content.push object.to_s
p " #{object.content}"
end
p '_____________'
bucket_list[bucket_name] = bucket_content
erb :index, locals: { whom: whom, container: container, buckets: bucket_list}
end
post '/upload' do
upload(params[:content]['file'][:filename], params[:content]['file'][:tempfile])
redirect '/'
end
helpers do
def upload(filename, file)
bucket = ENV['S3_BUCKET_NAME'] || 'deis-store.local3.deisapp.com/fancy_new_bucket'
AWS::S3::Base.establish_connection!(
server: ENV['S3_HOST'] || 'deis-store.local3.deisapp.com',
port: ENV['S3_PORT'],
access_key_id: ENV['S3_ACCESS_KEY_ID'],
secret_access_key: ENV['S3_SECRET_ACCESS_KEY']
)
AWS::S3::S3Object.store(
filename,
open(file.path),
bucket
)
return filename
end
end