-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathbucket_website.js
94 lines (82 loc) · 2.81 KB
/
bucket_website.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
});
}
});
}
});
// 设置存储桶静态网站
function putBucketWebsite() {
//.cssg-snippet-body-start:[put-bucket-website]
cos.putBucketWebsite({
Bucket: 'examplebucket-1250000000', /* 必须 */
Region: 'ap-beijing', /* 必须 */
WebsiteConfiguration: {
IndexDocument: {
Suffix: "index.html"
},
ErrorDocument: {
Key: "error.html"
},
RedirectAllRequestsTo: {
Protocol: "https"
},
}
}, function(err, data) {
console.log(err || data);
});
//.cssg-snippet-body-end
}
// 获取存储桶静态网站
function getBucketWebsite() {
//.cssg-snippet-body-start:[get-bucket-website]
cos.getBucketWebsite({
Bucket: 'examplebucket-1250000000', /* 必须 */
Region: 'ap-beijing', /* 必须 */
}, function(err, data) {
console.log(err || data);
});
//.cssg-snippet-body-end
}
// 删除存储桶静态网站
function deleteBucketWebsite() {
//.cssg-snippet-body-start:[delete-bucket-website]
cos.deleteBucketWebsite({
Bucket: 'examplebucket-1250000000', /* 必须 */
Region: 'ap-beijing', /* 必须 */
}, function(err, data) {
console.log(err || data);
});
//.cssg-snippet-body-end
}
//.cssg-methods-pragma
function callBucketWebsite() {
// 设置存储桶静态网站
putBucketWebsite()
// 获取存储桶静态网站
getBucketWebsite()
// 删除存储桶静态网站
deleteBucketWebsite()
//.cssg-methods-pragma
}