Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
李凯 committed Mar 11, 2016
0 parents commit 2141341
Show file tree
Hide file tree
Showing 527 changed files with 100,664 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rvm:
- 1.8.7
- 1.9.3
- 1.9.2
- 2.0.0
- 2.1.5

script: "bundle exec rake"
env: "CI=1 SUITE=1 "
bundler_args: "--without repl documentation release"
9 changes: 9 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--title 'MSS SDK for Ruby'
--output-dir api-docs
--template-path doc-src/templates
--markup markdown
--markup-provider rdiscount
--hide-api private
--plugin sitemap
-e doc-src/api_docs_plugin.rb
lib/**/*.rb
44 changes: 44 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

source 'https://rubygems.org'

gemspec :name => 'mss-sdk'

gem 'rake'
gem 'nokogiri', '< 1.6'

group :repl do
gem 'pry'
end

group :documentation do
gem 'yard', :git => 'https://github.com/trevorrowe/yard.git', :branch => 'frameless'
gem 'rdiscount', :github => 'lsegal/rdiscount', :branch => 'gfm-fenced-code'
gem 'yard-sitemap', '~> 1.0'
gem 'rdoc', '= 3.9.4'
end

group :test do
gem 'rspec', '~> 2.12'
gem 'cucumber', '< 2.0'
gem 'simplecov', :require => false
gem 'net-ssh', '~> 2.1'
gem 'multipart-post'
gem 'rotp', '~> 1.3.0'
end

if RUBY_VERSION >= '1.9.2'
group :release do
gem 'octokit'
end
end
Empty file added LICENSE.txt
Empty file.
192 changes: 192 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# MSS(Meituan Storage Service) SDK for Ruby

This is MSS SDK for Ruby.

## Introduction

### MSS服务介绍
美团云存储服务(Meituan Storage Service, 简称MSS),是美团云对外提供的云存储服务,其具备高可靠,安全,低成本等特性,并且其API兼容S3。MSS适合存放非结构化的数据,比如图片,视频,文档,备份等。

### MSS基本概念介绍
* MSS的API兼容S3, 其基本概念也和S3相同,主要包括Object, Bucket, Access Key, Secret Key等。

* Object对应一个文件,包括数据和元数据两部分。元数据以key-value的形式构成,它包含一些默认的元数据信息,比如Content-Type, Etag等,用户也可以自定义元数据。

* Bucket是object的容器,每个object都必须包含在一个bucket中。用户可以创建任意多个bucket。

* Access Key和Secret Key: 用户注册MSS时,系统会给用户分配一对Access Key和Secret Key, 用于标识用户,用户在使用API使用MSS服务时,需要使用这两个Key。请在美团云管理控制台查询AccessKey和SecretKey。

### MSS访问域名

```
mtmss.com
```

## Installation

安装MSS SDK for Ruby,需要ruby与gem,并且ruby版本在1.9.3以上。

```
# Build MSS SDK for Ruby Gem
gem build mss-sdk.gemspec
# Install MSS SDK for Ruby Gem
gem install -l mss-sdk-1.0.0.gem
```

## Quick Start

##### 初始化

```ruby
require 'mss-sdk'
s3 = MSS::S3.new({
:s3_endpoint => 'mtmss.com',
:use_ssl => false,
:s3_force_path_style => true,
:access_key_id => '****Access Key****',
:secret_access_key => '****Access Secret****'})
```

##### 新建bucket

```ruby
bucket = s3.buckets.create('bucket_name')
```

##### 列出所有bucket

```ruby
s3.buckets.each do |bucket|
puts bucket.name
end
```

##### 设置bucket属性为公共可读

```ruby
bucket.set_acl_public_read
```

##### 设置bucket属性为私有

```ruby
bucket.set_acl_private
```

##### 判断bucket是否存在

```ruby
bucket.exists?
```

##### 从字符串或缓冲区上传对象

```ruby
object_name_one = 'object1'
object_content = 'test'
obj = bucket.objects[object_name_one].write(object_content)
```

##### 删除对象

```ruby
obj.delete
```

##### 从文件上传对象

```ruby
object_name_for_test_upload = 'object2'
upload_file_path = 'filepath'
obj_upload = bucket.objects[object_name_for_test_upload]
obj_upload.write(:file => upload_file_path)
```

##### 下载对象到本地文件

```ruby
File.open('output', 'wb') do |file|
obj_upload.read do |chunk|
file.write(chunk)
end
end
```

##### 生成预签名的对象地址

```ruby
temp_url_for_read = obj_upload.url_for(:read, {:expire => 600})
puts temp_url_for_read
```

##### 删除bucket内所有对象

```ruby
bucket.clear!
```

##### 删除bucket

```ruby
bucket.delete
```

##### 大文件分片上传

```ruby
upload = bucket.objects["Hello美团云!"].multipart_upload
upload.add_part("a" * 5242880)
upload.add_part("b" * 2097152)
upload.complete()
puts "Multipart upload succ, upload id:" + upload.id
```

## 预签名Post上传对象

```
请参考samples/s3/presinged_post.rb
```

##### 服务器端生成签名表单,用于发给客户端

```ruby
post_info_str = s3.presigned_post_info(
"share", # bucket名字
{
:expires => 300, # 签名有效期,单位秒
:metadata => {"x-amz-meta-server" => "Hello Server!"}, # 服务器端自定义的变量,必须以"x-amz-meta-"为前缀
:callback_url => "http://mtmsscb.mtmss.cn", # 上传成功后的回调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", # 上传成功后回调的Content-Type
:callback_host => "mtmsscb.mtmss.com" # 上传成功后回调http header中的host
}).to_json
```

##### 目前支持的魔法变量
| 名字 | 描述 |
|--------|----------------------|
| bucket | bucket名字 |
| key | 对象名字 |
| etag | 对象内容的md5sum |
| fname | 上传表单中的filename |
| fsize | 对象大小 |

##### 客户端使用Post上传对象

```ruby
# 这里使用ruby的rest-client做为示例
client_info = {
"x-amz-meta-client" => "Hello Client!", # 客户端自定义变量,mss遵守标准S3协议,post表单最后一项必须是对象内容,因此客户端自定义的变量要写在value之前
:key => "Key is lena.jpg", # 对象名字
:value => File.new("./lena.jpg", 'rb'), # 待上传的对象内容
}
post_info_obj = JSON.parse(post_info_str) # post_info_str为服务器端生成的签名表单对象,包括url和form,其中form为表单内容,url为上传要用到的url
RestClient.post post_info_obj["url"], post_info_obj["form"].merge(client_info) # 与客户端自定义的表单内容合并后使用rest-client上传
```

##### 回调服务器收到的消息体

```
name=lena.jpg&bucket=share&key=Key is lena.jpg&hash="76d710edc4cf48d84e3cfc7e24234a09"&size=68261&server=Hello Server!&client=Hello Client!
```
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$REPO_ROOT = File.dirname(__FILE__)

$VERSION = ENV['VERSION'] || File.read(File.join($REPO_ROOT, 'VERSION')).strip

task 'test:coverage:clear' do
sh("rm -rf #{File.join($REPO_ROOT, 'coverage')}")
end

Dir.glob('**/*/**/*.rake').each do |task_file|
load task_file
end

task :default => :test
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
Loading

0 comments on commit 2141341

Please sign in to comment.