netstorageapi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/akamai/netstorage.rb +181 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c8c3c41f92b0ddb4cb562929eb3692fd7628c2d
4
+ data.tar.gz: ac5be83acb9c78c59d2c8e4a369d51aab730992d
5
+ SHA512:
6
+ metadata.gz: 1802526fdbfee477083cf3773e85c3eb01ff71f1761e9a522e82d860f00992ea622ee626fb5b50463d4dfa8f4e9653a14b1589e1a412701c73ed3e16b8ae4a39
7
+ data.tar.gz: fc6a863f02e1e0e7ac96003d73db8fbc33f5957c355a4b20f0adf3675e1ec22f5bfcddfdd1e3b496e43539a0b2126dd0041f76e5dce67c4cc3dcbcacb0b3cc7d
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Original author: Astin Choi <achoi@akamai.com>
4
+
5
+ # Copyright 2016 Akamai Technologies http://developer.akamai.com.
6
+
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+
20
+ require "base64"
21
+ require "cgi"
22
+ require "net/http"
23
+ require "openssl"
24
+ require "uri"
25
+
26
+
27
+ class Netstorage
28
+
29
+ attr_accessor :hostname, :keyname, :key
30
+
31
+ def initialize(hostname, keyname, key)
32
+ @hostname = hostname
33
+ @keyname = keyname
34
+ @key = key
35
+ end
36
+
37
+ private
38
+
39
+ def _response(request, uri, kwargs)
40
+ if kwargs[:action] == "download"
41
+ local_destination = kwargs[:destination]
42
+ if kwargs[:path]
43
+ ns_filename = kwargs[:path][-1] != '/' ? File.basename(kwargs[:path]) : nil
44
+ if local_destination == ''
45
+ local_destination = ns_filename
46
+ elsif File.directory?(local_destination)
47
+ local_destination = File.join(local_destination, ns_filename)
48
+ end
49
+ end
50
+ response = Net::HTTP.start(uri.hostname, uri.port) { |http|
51
+ http.request request do |res|
52
+ open(local_destination, "wb") do |io|
53
+ res.read_body do |chunk|
54
+ io.write chunk
55
+ end
56
+ end
57
+ end
58
+ }
59
+ elsif kwargs[:action] == "upload"
60
+ request.body = File.read(kwargs[:source])
61
+ response = Net::HTTP.start(uri.hostname, uri.port) { |http|
62
+ http.request(request)
63
+ }
64
+ else
65
+ response = Net::HTTP.start(uri.hostname, uri.port) { |http|
66
+ http.request(request)
67
+ }
68
+ end
69
+ return response
70
+ end
71
+
72
+ def _request(kwargs={})
73
+ path = URI::escape(kwargs[:path])
74
+ acs_action = "version=1&action=#{kwargs[:action]}"
75
+ acs_auth_data = "5, 0.0.0.0, 0.0.0.0, #{Time.now.to_i}, #{Random.rand(100000)}, #{@keyname}"
76
+ sign_string = "#{path}\nx-akamai-acs-action:#{acs_action}\n"
77
+ message = acs_auth_data + sign_string
78
+
79
+ hash_ = OpenSSL::HMAC.digest("sha256", @key, message)
80
+ acs_auth_sign = Base64.encode64(hash_).rstrip
81
+
82
+ uri = URI("http://#{@hostname}#{path}")
83
+ headers = {
84
+ 'X-Akamai-ACS-Action' => acs_action,
85
+ 'X-Akamai-ACS-Auth-Data' => acs_auth_data,
86
+ 'X-Akamai-ACS-Auth-Sign' => acs_auth_sign,
87
+ 'Accept-Encoding' => "identity"
88
+ }
89
+
90
+ if kwargs[:method] == "GET"
91
+ request = Net::HTTP::Get.new(uri, initheader=headers)
92
+ elsif kwargs[:method] == "POST"
93
+ request = Net::HTTP::Post.new(uri, initheader=headers)
94
+ elsif kwargs[:method] == "PUT" # Use only upload
95
+ request = Net::HTTP::Put.new(uri, initheader=headers)
96
+ end
97
+
98
+ response = _response(request, uri, kwargs)
99
+
100
+ return response.code == "200", response
101
+ end
102
+
103
+ public
104
+
105
+ def dir(ns_path)
106
+ return _request(action: "dir&format=xml",
107
+ method: "GET",
108
+ path: ns_path)
109
+ end
110
+
111
+ def download(ns_source, local_destination='')
112
+ return _request(action: "download",
113
+ method: "GET",
114
+ path: ns_source,
115
+ destination: local_destination)
116
+ end
117
+
118
+ def du(ns_path)
119
+ return _request(action: "du&format=xml",
120
+ method: "GET",
121
+ path: ns_path)
122
+ end
123
+
124
+ def stat(ns_path)
125
+ return _request(action: "stat&format=xml",
126
+ method: "GET",
127
+ path: ns_path)
128
+ end
129
+
130
+ def mkdir(ns_path)
131
+ return _request(action: "mkdir",
132
+ method: "POST",
133
+ path: ns_path)
134
+ end
135
+
136
+ def rmdir(ns_path)
137
+ return _request(action: "rmdir",
138
+ method: "POST",
139
+ path: ns_path)
140
+ end
141
+
142
+ def mtime(ns_path, mtime)
143
+ return _request(action: "mtime&format=xml&mtime=#{mtime}",
144
+ method: "POST",
145
+ path: ns_path)
146
+ end
147
+
148
+ def delete(ns_path)
149
+ return _request(action: "delete",
150
+ method: "POST",
151
+ path: ns_path)
152
+ end
153
+
154
+ def quick_delete(ns_path)
155
+ return _request(action: "quick-delete&quick-delete=imreallyreallysure",
156
+ method: "POST",
157
+ path: ns_path)
158
+ end
159
+
160
+ def rename(ns_target, ns_destination)
161
+ return _request(action: "rename&destination=#{CGI::escape(ns_destination)}",
162
+ method: "POST",
163
+ path: ns_target)
164
+ end
165
+
166
+ def symlink(ns_target, ns_destination)
167
+ return _request(action: "symlink&target=#{CGI::escape(ns_target)}",
168
+ method: "POST",
169
+ path: ns_destination)
170
+ end
171
+
172
+ def upload(local_source, ns_destination)
173
+ if File.file?(local_source) && ns_destination[-1] == "/"
174
+ ns_destination = "#{ns_destination}#{File.basename(local_source)}"
175
+ end
176
+ return _request(action: "upload",
177
+ method: "PUT",
178
+ source: local_source,
179
+ path: ns_destination)
180
+ end
181
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netstorageapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Astin Choi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: NetstorageAPI is Akamai Netstorage (File/Object Store) API for Ruby 1.9+
14
+ email: achoi@akamai.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/akamai/netstorage.rb
20
+ homepage: https://github.com/akamai-open/NetstorageKit-Ruby
21
+ licenses:
22
+ - Apache
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.9.3
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Akamai Netstorage API for Ruby
44
+ test_files: []