Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持生成上传url #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,41 @@ upyun.upload(File.new('filepath.png'))
* `sign`: 签名参数,详情见 [sign与non-sign参数说明](http://docs.upyun.com/api/form_api/#note6)
* 如果在请求中指定了 `ext-param`, 那么返回的结构中也会有 `ext-param` 字段,详情见 [ext-param](http://docs.upyun.com/api/form_api/#note5)

##### 生成上传URL
客户端可以直接上传文件到upyun(而不先上传自己的主机再传到upyun)。

```ruby
def upyun_upload_url
file_key = SecureRandom.uuid
upyun = Upyun::Form.new('form-api-secret', 'bucket')
upyun_json = upyun.generate_upload_url('save-key'=> file_key,'content-type'=>'audio/mp3')

remote_link = 'https://' + bucket + '.b0.upaiyun.com/'+file_key
upload_url = 'https://v0.api.upyun.com/' + bucket
render json: { remoteLink: remote_link,
uploadUrl: upload_url,
policy: upyun_json[:policy],
signature: upyun_json[:signature] }
end
```

```javascript
$.get('/user/'+id+'/audio/upyun_upload_url', (function(question, response) {
var uploadData = new AudioData();
uploadData.append('signature', response.signature);
uploadData.append('policy', response.policy);
uploadData.append('file', mp3Blob);
$.ajax({
url: response.uploadUrl,
type: 'POST',
data: uploadData,
contentType: 'audio/mpeg',
processData: false
});
}
```


##### 自定义参数
可以在上传的时候指定一些策略参数:

Expand Down
22 changes: 16 additions & 6 deletions lib/upyun/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ def initialize(password, bucket, options={timeout: 60})
@endpoint = ED_AUTO
end

def upload(file, opts={})
base_opts = HashWithIndifferentAccess.new({
'bucket' => @bucket,
'save-key' => '/{year}/{mon}/{day}/{filename}{.suffix}',
'expiration' => Time.now.to_i + 600
})
def generate_upload_url(opts={})
payload = {
policy: policy(base_opts.merge(opts)),
signature: signature,
url: "#{base_opts['save-key']}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cgg5207 请问这个 url 字段在哪里使用到了?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/upyun/ruby-sdk/pull/7/files#diff-04c6e90faac2675aa89e2176d2eec7d8R307
file_key 就是url, 只是为了与你们返回格式统一用。

POST https://v0.api.upyun.com/prepsmith

code: 200
message: "ok"
sign: "4cfa73b094e21f17534ef8b496719af1"
time: 1449827023
url: "6965706-19a16b00-4455-4a60-854f-38a5fdcb9449"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里上面用的是 merge 方法,而不是 merge!,所以 save-key 可能被 opts 给覆盖了.

}
end

def upload(file, opts={})
payload = {
policy: policy(base_opts.merge(opts)),
signature: signature,
Expand Down Expand Up @@ -82,6 +84,14 @@ def upload(file, opts={})
end

private
def base_opts
HashWithIndifferentAccess.new({
'bucket' => @bucket,
'save-key' => '/{year}/{mon}/{day}/{filename}{.suffix}',
'expiration' => Time.now.to_i + 600
})
end

def policy(opts)
@_policy = Base64.strict_encode64(policy_json(opts))
end
Expand Down