临时密钥在api工具里面测试过是可以上传图片的,但是在小程序里面就会返回403 AccessDenied
我怀疑是跟postObject有关,是不是临时密钥要配置什么。看了postObject api没找到问题,有曾经踩坑的兄弟吗
前端代码
// 腾讯云配置
let cosConfig = {
Bucket: configs.cosConfig.Bucket,
Region: configs.cosConfig.Region,
Server: configs.cosConfig.Server,
SecretId: configs.cosConfig.SecretId,
SecretKey: configs.cosConfig.SecretKey
}
let cos = new COS({
getAuthorization: async (params, callback) => { //获取签名 必填参数
// 推荐(服务器计算签名接口)
await uni.request({
url: cosConfig.Server,
header:{token: uni.getStorageSync("token")},
success: function (result) {
var credentials = result.data.data
callback({
SecretId: credentials.TmpSecretId,
SecretKey: credentials.TmpSecretKey,
XCosSecurityToken: credentials.XCosSecurityToken,
StartTime: credentials.StartTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
ExpiredTime: credentials.ExpiredTime, // 时间戳,单位秒,如:1580000900
});
}
});
// 方便前端调试
// let authorization = COS.getAuthorization({
// SecretId: cosConfig.SecretId,
// SecretKey: cosConfig.SecretKey,
// Method: params.Method,
// Key: params.Key
// });
// callback(authorization);
}
});
let filePath = upload_picture_list[j]['path'];
let Key = filePath.substr(filePath.lastIndexOf('/') + 1); // 这里指定上传的文件名
console.log(cosConfig.Bucket)
console.log(cosConfig.Region)
console.log(Key)
cos.postObject({
Bucket: cosConfig.Bucket,
Region: cosConfig.Region,
Key: Key,
FilePath: filePath
}, (err, data) => {
if (err == null) {
console.log(`%c 腾讯云上传(成功返回地址):${data.headers.Location}`, 'color:#1AAD19');
upload_picture_list[j]['path_server'] = data.headers.Location;
upload_picture_list[j]['upload_percent'] = 100;
} else {
console.log(`%c 腾讯云上传失败:${JSON.stringify(err)}`, 'color:#f00');
return;
}
});
后端代码
public Result getUploadCredential(){
TreeMap<String, Object> config = new TreeMap<String, Object>();
try {
// 替换为您的 SecretId
config.put("SecretId", secretId);
// 替换为您的 SecretKey
config.put("SecretKey", secretKey);
// 临时密钥有效时长,单位是秒,默认1800秒,最长可设定有效期为7200秒
config.put("durationSeconds", 1800);
// 换成您的 bucket
config.put("bucket", bucketName);
// 换成 bucket 所在地区
config.put("region", region);
config.put("allowPrefix", "*");
// 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
String[] allowActions = new String[] {
// 简单上传
"name/cos:PutObject",
// 表单上传、小程序上传
"name/cos:PostObject",
// 分片上传
"name/cos:InitiateMultipartUpload",
"name/cos:ListMultipartUploads",
"name/cos:ListParts",
"name/cos:UploadPart",
"name/cos:CompleteMultipartUpload"
};
config.put("allowActions", allowActions);
JSONObject credential = CosStsClient.getCredential(config);
//成功返回临时密钥信息
System.out.println(credential);
Map<String, Object> result = new HashMap<>();
String tmpSecretId = credential.getJSONObject("credentials").getString("tmpSecretId");
String tmpSecretKey = credential.getJSONObject("credentials").getString("tmpSecretKey");
Long expiredTime = credential.getLong("expiredTime");
Long startTime = credential.getLong("startTime");
String sessionToken = credential.getJSONObject("credentials").getString("sessionToken");
result.put("TmpSecretId",tmpSecretId);
result.put("TmpSecretKey",tmpSecretKey);
result.put("ExpiredTime",expiredTime);
result.put("XCosSecurityToken",sessionToken);
result.put("StartTime",startTime);
return ApiResponse.success(result);
} catch (Exception e) {
//失败抛出异常
throw new IllegalArgumentException("no valid secret !");
}
}
相似问题