FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-sdk/examples/UploadBySelfDefiningParam.java at master · CDNCloud-com/java-sdk · GitHub
CDNCloud-com
/
java-sdk
Public
forked from
qiniu/java-sdk
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-sdk
/
examples
/
UploadBySelfDefiningParam.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
69 lines (60 loc) · 2.63 KB
Breadcrumbs
java-sdk
/
examples
/
UploadBySelfDefiningParam.java
Copy path
File metadata and controls
69 lines (60 loc) · 2.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import
com
.
qiniu
.
common
.
QiniuException
;
import
com
.
qiniu
.
http
.
Response
;
import
com
.
qiniu
.
storage
.
Configuration
;
import
com
.
qiniu
.
storage
.
Region
;
import
com
.
qiniu
.
storage
.
UploadManager
;
import
com
.
qiniu
.
util
.
Auth
;
import
com
.
qiniu
.
util
.
StringMap
;
/**
* 设置自定义变量上传并接收自定义变量 demo
*
* 自定义变量需要以 x: 开头, 携带自定义变量上传参考文档
* https://developer.qiniu.com/kodo/manual/1235/vars#2
*
* 接收自定义变量参考上传策略文档 -- returnBody
* https://developer.qiniu.com/kodo/manual/1206/put-policy
*
* 服务端具体用法实例参考 UploadBySelfDefiningParam.upload()
*/
public
class
UploadBySelfDefiningParam
{
private
static
final
String
ACCESS_KEY
=
"设置好你们自己的AK"
;
private
static
final
String
SECRET_KEY
=
"设置好你们自己的SK"
;
private
static
final
Auth
auth
=
Auth
.
create
(
ACCESS_KEY
,
SECRET_KEY
);
private
Configuration
cfg
;
private
UploadManager
uploadManager
;
private
Region
region
;
private
String
bucket
;
{
bucket
=
"设置你们自己的上传空间名称"
;
//指定存储空间所在区域,华北region1,华南region2 ,华东 region0
region
=
Region
.
region1
();
//初始化cfg实例,可以指定上传区域,也可以创建无参实例 , cfg = new Configuration();
cfg
=
new
Configuration
(
region
);
//是否指定https上传,默认true
//cfg.useHttpsDomains=false;
//构建 uploadManager 实例
uploadManager
=
new
UploadManager
(
cfg
);
}
public
static
void
main
(
String
args
[])
throws
Exception
{
UploadBySelfDefiningParam
up
=
new
UploadBySelfDefiningParam
();
up
.
upload
();
}
public
void
upload
()
throws
QiniuException
{
//设置上传后的文件名称
String
key
=
"qiniu_test.jpg"
;
//上传策略
StringMap
policy
=
new
StringMap
();
//自定义上传后返回内容,返回自定义参数,需要设置 x:参数名称
policy
.
put
(
"returnBody"
,
"{
\"
key
\"
:
\"
$(key)
\"
,
\"
hash
\"
:
\"
$(etag)
\"
,
\"
fname
\"
:
\"
$(x:fname)
\"
,
\"
age
\"
,$(x:age)}"
);
//生成上传token
String
upToken
=
auth
.
uploadToken
(
bucket
,
key
,
3600
,
policy
);
//上传自定义参数,自定义参数名称需要以 x:开头
StringMap
params
=
new
StringMap
();
params
.
put
(
"x:fname"
,
"123.jpg"
);
params
.
put
(
"x:age"
,
20
);
String
localFilePath
=
"/Users/mini/Downloads/qiniu_test.jpg"
;
Response
response
=
uploadManager
.
put
(
localFilePath
,
key
,
upToken
,
params
,
null
,
false
);
//输出返回结果
System
.
out
.
println
(
response
.
bodyString
());
}
}
Back
|
FazBrowse Home
|
New Git URL