upyun-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/LICENSE +22 -0
- data/README.md +112 -0
- data/Rakefile +2 -0
- data/lib/upyun/version.rb +3 -0
- data/lib/upyun-sdk.rb +127 -0
- data/upyun-sdk.gemspec +23 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: d5268c98f675b2fad464f99f0f9331b7158577f8
|
|
4
|
+
data.tar.gz: 54bf95dfc964ac8fe692825b12dff74d03cf1bae
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c497f45995b56a78d73413617a2da708327009ff16b2c04c3292c84bd1fc118b026c10a134428ab79b7dbed3881e1d575113ad34257784cd0d7f10251215d32a
|
|
7
|
+
data.tar.gz: 56e8600c52dda8308f61eafcaeda70517905475782d271b47060695bb4533c2f08be1afaeb982b28fa08b22b18cffc67a445d454a07a421f642835727586324f
|
data/.gitignore
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 calelelac
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# 又拍云 Ruby SDK
|
|
2
|
+
|
|
3
|
+
又拍云存储 Ruby SDK,基于 [又拍云存储 HTTP REST API 接口](http://wiki.upyun.com/index.php?title=HTTP_REST_API%E6%8E%A5%E5%8F%A3) 开发。
|
|
4
|
+
|
|
5
|
+
### 安装说明
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
gem install upyun-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 基本函数接口
|
|
12
|
+
|
|
13
|
+
### 初始化 UpYun
|
|
14
|
+
|
|
15
|
+
````
|
|
16
|
+
require 'upyun-sdk'
|
|
17
|
+
|
|
18
|
+
up = UpYun.new('bucket', 'username', 'password', 0)
|
|
19
|
+
````
|
|
20
|
+
|
|
21
|
+
其中,参数 `bucket` 为空间名称,`username` 和 `password` 分别为授权操作员帐号和密码,必选。
|
|
22
|
+
|
|
23
|
+
根据国内的网络情况,又拍云存储 API 目前提供了电信、联通网通、移动铁通三个接入点,在初始化时可由参数 `endpoint` 进行设置,其可选的值有:
|
|
24
|
+
|
|
25
|
+
````
|
|
26
|
+
0 根据网络条件自动选择接入点,默认
|
|
27
|
+
1 电信接入点
|
|
28
|
+
2 联通网通接入点
|
|
29
|
+
3 移动铁通接入点
|
|
30
|
+
````
|
|
31
|
+
|
|
32
|
+
默认设置为 `0` , 但是我们推荐根据服务器网络状况,手动设置合理的接入点以获取最佳的访问速度。。同时,也可通过:
|
|
33
|
+
|
|
34
|
+
````
|
|
35
|
+
up.endpoint = 1
|
|
36
|
+
````
|
|
37
|
+
|
|
38
|
+
在对象使用过程中更改。
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### 上传文件
|
|
42
|
+
|
|
43
|
+
#### 直接传递文件内容的形式上传
|
|
44
|
+
|
|
45
|
+
````
|
|
46
|
+
up.put('/upyun-sdk/ascii.txt', 'abcdefghijklmnopqrstuvwxyz\n')
|
|
47
|
+
````
|
|
48
|
+
|
|
49
|
+
其中,方法 `up.put` 默认已开启相应目录的自动创建。
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
其中,参数 `auto_mkdir` 默认为 true,表示自动创建目录;`checksum`,默认 False,表示不进行 MD5 校验; `headers` ,可根据需求设置自定义 HTTP Header,例如作图参数 x-gmkerl-*, 具体请参考 [标准 API 上传文件](http://wiki.upyun.com/index.php?title=%E6%A0%87%E5%87%86API%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6) 。
|
|
53
|
+
|
|
54
|
+
上传成功,返回 true; 失败返回 false。
|
|
55
|
+
|
|
56
|
+
### 下载文件
|
|
57
|
+
|
|
58
|
+
#### 直接读取文件内容
|
|
59
|
+
|
|
60
|
+
````
|
|
61
|
+
res = up.get('/upyun-sdk/ascii.txt')
|
|
62
|
+
````
|
|
63
|
+
|
|
64
|
+
下载成功,返回文件内容; 失败则返回错误信息。
|
|
65
|
+
|
|
66
|
+
### 获取文件信息
|
|
67
|
+
|
|
68
|
+
````
|
|
69
|
+
res = up.getinfo('/upyun-sdk/xinu.png')
|
|
70
|
+
puts res['file-type']
|
|
71
|
+
puts res['file-size']
|
|
72
|
+
puts res['file-date']
|
|
73
|
+
````
|
|
74
|
+
|
|
75
|
+
获取成功,返回一个 Hash 对象; 失败则返回错误信息。
|
|
76
|
+
|
|
77
|
+
### 创建目录
|
|
78
|
+
|
|
79
|
+
````
|
|
80
|
+
up.mkdir('/upyun-sdk/temp/')
|
|
81
|
+
````
|
|
82
|
+
|
|
83
|
+
其中,参数 `auto_mkdir` 默认为 true,表示自动创建目录;
|
|
84
|
+
创建成功,返回 true; 失败,返回 false。
|
|
85
|
+
|
|
86
|
+
### 删除目录或文件
|
|
87
|
+
|
|
88
|
+
````
|
|
89
|
+
up.delete('/upyun-sdk/xinu.png')
|
|
90
|
+
up.delete('/upyun-sdk/temp/')
|
|
91
|
+
````
|
|
92
|
+
|
|
93
|
+
删除成功,返回 true; 失败返回 false。注意删除目录时,必须保证目录为空。
|
|
94
|
+
|
|
95
|
+
### 获取目录文件列表
|
|
96
|
+
|
|
97
|
+
````
|
|
98
|
+
res = up.getlist('/upyun-sdk/')
|
|
99
|
+
````
|
|
100
|
+
|
|
101
|
+
获取成功,返回一个包含该目录下所有目录或文件条目信息的 Hash 对象。获取失败,则返回错误信息。
|
|
102
|
+
该方法默认获取根目录列表信息。
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
### 获取空间使用情况
|
|
106
|
+
|
|
107
|
+
````
|
|
108
|
+
res = up.usage()
|
|
109
|
+
````
|
|
110
|
+
|
|
111
|
+
获取成功,始终返回该空间当前使用的总容量,单位 Bytes; 失败则返回错误信息。
|
|
112
|
+
该方法默认获取根目录空间使用情况。
|
data/Rakefile
ADDED
data/lib/upyun-sdk.rb
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# 默认 upyun 签名
|
|
2
|
+
require 'digest/md5'
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
class UpYun
|
|
7
|
+
attr_accessor :bucket, :username, :password
|
|
8
|
+
attr_reader :endpoint
|
|
9
|
+
|
|
10
|
+
def initialize(bucket, username, password, endpoint=nil)
|
|
11
|
+
@bucket = bucket
|
|
12
|
+
@username = username
|
|
13
|
+
@password = Digest::MD5.hexdigest(password)
|
|
14
|
+
self.endpoint = endpoint || 0
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# public API
|
|
18
|
+
|
|
19
|
+
def endpoint=(value)
|
|
20
|
+
@endpoint = "v#{value}.api.upyun.com"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# 上传文件
|
|
24
|
+
def put(path, data, auto_mkdir=true, checksum=nil, headers=nil)
|
|
25
|
+
headers = headers || {}
|
|
26
|
+
headers['mkdir'] = !!auto_mkdir
|
|
27
|
+
headers['Content-MD5'] = checksum if checksum
|
|
28
|
+
headers['Content-Length'] = data.size.to_s
|
|
29
|
+
res = http_request('PUT', path, data, headers)
|
|
30
|
+
res_ok?(res)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# 下载文件
|
|
34
|
+
def get(path)
|
|
35
|
+
res = http_request('GET', path)
|
|
36
|
+
res_ok?(res) ? res.body : res_errmsg(res)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# 获取文件信息
|
|
40
|
+
def getinfo(path)
|
|
41
|
+
res = http_request('HEAD', path)
|
|
42
|
+
if res_ok?(res)
|
|
43
|
+
return {
|
|
44
|
+
:file_type => res['x-upyun-file-type'],
|
|
45
|
+
:file_size => res['x-upyun-file-size'],
|
|
46
|
+
:file_date => res['x-upyun-file-date']
|
|
47
|
+
}
|
|
48
|
+
end
|
|
49
|
+
res_errmsg(res)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# 创建目录
|
|
53
|
+
def mkdir(path, auto_mkdir=true)
|
|
54
|
+
headers = {'folder' => 'true'}
|
|
55
|
+
headers['mkdir'] = !!auto_mkdir
|
|
56
|
+
res = http_request('POST', path, nil, headers)
|
|
57
|
+
res_ok?(res)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# 删除目录或文件
|
|
61
|
+
def delete(path)
|
|
62
|
+
res = http_request('DELETE', path)
|
|
63
|
+
res_ok?(res)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# 获取目录文件列表
|
|
67
|
+
def getlist(path='/')
|
|
68
|
+
res = http_request('GET', path)
|
|
69
|
+
if res_ok?(res)
|
|
70
|
+
list = res.body.split("\n").map do |file|
|
|
71
|
+
name, type, size, time = file.split("\t")
|
|
72
|
+
{
|
|
73
|
+
:name => name,
|
|
74
|
+
:type => type,
|
|
75
|
+
:size => size,
|
|
76
|
+
:time => time
|
|
77
|
+
}
|
|
78
|
+
end
|
|
79
|
+
return list
|
|
80
|
+
end
|
|
81
|
+
res_errmsg(res)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# 获取空间使用情况
|
|
85
|
+
def usage(path='/')
|
|
86
|
+
res = http_request('GET', "#{path}?usage")
|
|
87
|
+
res_ok?(res) ? res.body : res_errmsg(res)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def res_ok?(res)
|
|
93
|
+
res.code.eql? '200'
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def res_errmsg(res)
|
|
97
|
+
"#{res.code} #{res.message}"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def http_request(method, path, data=nil, headers=nil)
|
|
101
|
+
base = "http://#{@endpoint}/#{@bucket}"
|
|
102
|
+
url = File.join(base, path)
|
|
103
|
+
uri = URI.parse(URI.encode(url))
|
|
104
|
+
|
|
105
|
+
headers = headers || {}
|
|
106
|
+
date = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
|
107
|
+
length = headers['Content-Length'] || 0
|
|
108
|
+
headers['Date'] = date
|
|
109
|
+
headers['Authorization'] = make_signature(method, uri.path, date, length)
|
|
110
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
|
111
|
+
if method == 'HEAD'
|
|
112
|
+
return http.head(uri.request_uri, headers)
|
|
113
|
+
else
|
|
114
|
+
return http.send_request(method, uri.request_uri, data, headers)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def make_signature(method, uri, date, length)
|
|
120
|
+
signature = "#{method}&#{uri}&#{date}&#{length}&#{md5(@password)}"
|
|
121
|
+
"UpYun #{@username}:#{md5(signature)}"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def md5(data)
|
|
125
|
+
Digest::MD5.hexdigest(data)
|
|
126
|
+
end
|
|
127
|
+
end
|
data/upyun-sdk.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'upyun/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "upyun-sdk"
|
|
8
|
+
spec.version = Upyun::VERSION
|
|
9
|
+
spec.authors = ["woo"]
|
|
10
|
+
spec.email = ["kl.woohoo@gmail.com"]
|
|
11
|
+
spec.description = %q{Upyun SDK for Ruby. }
|
|
12
|
+
spec.summary = %q{Upyun SDK for Ruby. }
|
|
13
|
+
spec.homepage = "https://github.com/klwoohoo/upyun-sdk"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($\)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: upyun-sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- woo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
description: 'Upyun SDK for Ruby. '
|
|
42
|
+
email:
|
|
43
|
+
- kl.woohoo@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- .gitignore
|
|
49
|
+
- LICENSE
|
|
50
|
+
- README.md
|
|
51
|
+
- Rakefile
|
|
52
|
+
- lib/upyun-sdk.rb
|
|
53
|
+
- lib/upyun/version.rb
|
|
54
|
+
- upyun-sdk.gemspec
|
|
55
|
+
homepage: https://github.com/klwoohoo/upyun-sdk
|
|
56
|
+
licenses:
|
|
57
|
+
- MIT
|
|
58
|
+
metadata: {}
|
|
59
|
+
post_install_message:
|
|
60
|
+
rdoc_options: []
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - '>='
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - '>='
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
requirements: []
|
|
74
|
+
rubyforge_project:
|
|
75
|
+
rubygems_version: 2.0.6
|
|
76
|
+
signing_key:
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Upyun SDK for Ruby.
|
|
79
|
+
test_files: []
|