|
6 月之前 | |
---|---|---|
.. | ||
demo | 6 月之前 | |
demo-album | 6 月之前 | |
lib | 6 月之前 | |
server | 6 月之前 | |
src | 6 月之前 | |
LICENSE | 6 月之前 | |
README.md | 6 月之前 | |
build.js | 6 月之前 | |
index.js | 6 月之前 | |
package.json | 6 月之前 |
微信小程序 SDK for 腾讯云对象存储服务
由于签名计算放在前端会暴露 SecretId 和 SecretKey,我们把签名计算过程放在后端实现,前段通过 ajax 向后端获取签名结果,正式部署时请再后端加一层自己网站本身的权限检验。
这里提供 PHP 和 NodeJS 的签名例子,其他语言,请参照对应的 XML SDK
var Bucket = 'test-1250000000';
var Region = 'ap-guangzhou';
// 初始化实例
var cos = new COS({
getAuthorization: function (options, callback) {
// 异步获取签名
wx.request({
url: 'https://example.com/sts.php', // 步骤二提供的签名接口
data: {
Method: options.Method,
Key: options.Key
},
dataType: 'text',
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
});
}
});
}
});
// 选择文件
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original'], // 可以指定是原图还是压缩图,默认用原图
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var filePath = res.tempFiles[0].path;
var filename = filePath.substr(filePath.lastIndexOf('/') + 1);
cos.postObject({
Bucket: Bucket,
Region: Region,
Key: filename,
FilePath: filePath,
onProgress: function (info) {
console.log(JSON.stringify(info));
}
}, function (err, data) {
console.log(err || data);
});
}
});
完整文档有待完善,不支持分片相关接口,上传下载相关方法请查看源码和例子 使用例子
其他大部分接口可以参考以下 JS SDK 文档 接口文档