-
Notifications
You must be signed in to change notification settings - Fork 58
/
post_object.js
56 lines (50 loc) · 1.91 KB
/
post_object.js
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
var COS = require('../lib/cos-wx-sdk-v5');
var cos = new COS({
// ForcePathStyle: true, // 如果使用了很多存储桶,可以通过打开后缀式,减少配置白名单域名数量,请求时会用地域域名
getAuthorization: function (options, callback) {
// 异步获取临时密钥
wx.request({
url: 'https://example.com/server/sts.php',
data: {
bucket: options.Bucket,
region: options.Region,
},
dataType: 'json',
success: function (result) {
var data = result.data;
var credentials = data && data.credentials;
if (!data || !credentials) return console.error('credentials invalid');
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
// 建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
StartTime: data.startTime, // 时间戳,单位秒,如:1580000000
ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
});
}
});
}
});
// POST 方式上传对象
function postObject() {
//.cssg-snippet-body-start:[post-object]
cos.postObject({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: filename,
FilePath: tmpFilePath, // wx.chooseImage 选择文件得到的 tmpFilePath
onProgress: function (info) {
console.log(JSON.stringify(info));
}
}, function (err, data) {
console.log(err || data);
});
//.cssg-snippet-body-end
}
//.cssg-methods-pragma
function callPostObject() {
// POST 方式上传对象
postObject()
//.cssg-methods-pragma
}