This repository has been archived by the owner on Feb 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
presinged_post.rb
54 lines (48 loc) · 1.85 KB
/
presinged_post.rb
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
require 'mss-sdk'
require 'rest-client'
(access_key, secret_key, bucket_name, object_key, file_name, callback_url) = ARGV
unless access_key && secret_key && bucket_name && object_key && file_name && callback_url
puts "Usage: presigned_pose.rb <ACCESS_KEY> <SECRET_KEY> <BUCKET_NAME> <OBJECT_KEY> <FILE_NAME> <CALLBACK_URL>"
exit 1
end
# Server End
s3 = MSS::S3.new({
#:s3_endpoint => 'mtmss.com',
:s3_endpoint => 'msstest-corp.sankuai.com',
:use_ssl => false,
:s3_force_path_style => true,
:access_key_id => access_key,
:secret_access_key => secret_key})
post_info_str = s3.presigned_post_info(
bucket_name, #bucket name
{
:expires => 864000,
:callback_url => callback_url,
:callback_body => "name=${fname}&bucket=${bucket}&key=${key}&hash=${etag}&size=${fsize}&server=${x-amz-meta-server}&client=${x-amz-meta-client}",
:callback_body_type => "application/x-www-form-urlencoded",
}).to_json
# Client End
client_info = {
"x-amz-meta-client" => "Hello Client!",
"key" => object_key,
"value" => File.new(file_name, 'rb'),
}
post_info_obj = JSON.parse(post_info_str)
RestClient.post post_info_obj["url"], post_info_obj["form"].merge(client_info)
####################################################################################################
token = s3.presigned_post_token(
bucket_name, #bucket name
{
:expires => 864000,
:callback_url => callback_url,
:callback_body => "name=${fname}&bucket=${bucket}&key=${key}&hash=${etag}&size=${fsize}&server=${x-amz-meta-server}&client=${x-amz-meta-client}",
:callback_body_type => "application/x-www-form-urlencoded",
})
new_post_info = {
"key" => object_key,
"token" => token,
"x-amz-meta-client" => "Hello Client!",
"value" => File.new(file_name, 'rb'),
}
RestClient.post post_info_obj["url"], new_post_info
puts new_post_info.to_json