# 获取腾讯云cos配置
使用说明:
1、先调用该接口拿到腾讯云cos的临时密钥信息。
2、参考腾讯云对象存储SDK文档 (opens new window)并下载对应的SDK,调用腾讯云的接口把业务方的图片、文件、视频上传到腾讯云
2、调用开放平台消息接口,把图片、文件、视频的消息类型发到知音楼里面(请参考:消息类型与数据格式)
# host: https://yach-oapi.zhiyinlou.com 正式环境
# host: https://yach-oapi-test.zhiyinlou.com 测试环境
# 概述
名称 | 值 |
---|---|
地址 | /open/sts/get?access_token=ACCESS_TOKEN |
请求方式 | POST |
参数 | 见【参数】 |
返回值 | 见【返回值】 |
# 参数
参数 | 参数类型 | 必须 | 说明 |
---|---|---|---|
access_token | String | 是 | 调用接口凭证 |
type | String | 是 | 文件类型,有3种类型(图片和视频:image,私密(office、pdf文件):security,文件(普通文件):file) |
返回值
{
"code": 200,
"msg": "",
"obj": {
"expiredTime": 1598600413,
"expiration": "2020-08-28T07:40:13Z",
"credentials": {
"stsToken": "8p50naf39YFC3cyAKOnE3awD4UqzOs8Ad797c22c4a0a12927140ffb1c78ab0e42cWHF2G5_vhgf0sIX05kPj2jorJU6ECh_GS2N1zeN1eHF14dMNymZxRiADMArIpx0gAOiE7NNapINj726zzKWOkM1-VKLHGIL998654BKBONO-X8PwTnqQKe22jQ5Ddc-Yv6aPRGPbBgGESbphNAutdJ0S82NJm37gsfsYoWxWyPMbM2_JYu3B1M3001QrMvdcAbJ45nfd0L6d6--6QBvIKbLD06iCTUW1XcdFCCsYm_Y4FrQYLC4LzaOUE_lzd0ZbO2LrY5p5PS1RvxKvRJ1hU2mO_q4AtWVJCcLRGh2PDAhTvc5r2kwYffuOzjju89xCMIXzyJXi5QmerBF3dQ4--ADGsr834-xB47vdSXyOXQMSY_FHpWf4whNUJM8lE5jG1J7q0OC923G0iqydLwhNKnNE3IV947HRNR1w5CNn-KXhREZc4VA78QZf7Z1zetR7qKuux5fETEC26U8vwJZSj0OG_-q",
"tmpSecretId": "AKIDV3ZPn6K10RcfxzJhfgosihg08749_b8PGEV3h8rrD2utvf7dyrLnOs8x80EyQBJz9r",
"tmpSecretKey": "aJEEPB13EGIkljjoiyhsg2sdf/lkvOz5Slc="
},
"requestId": "5970a728-222b-d205-b708-b129d895d5f8",
"startTime": 1598593213,
"bucket": "yach-1256123776",
"region": "ap-beijing",
"domain": "yach-1256037416.cos.accelerate.myqcloud.com", // 文件上传成功后的域名地址
"key": "/dev/openApi/12345678/" //要上传到腾讯云cos的文件路径,也就是腾讯云对象存储SDK里面的Key字段
}
}
# 腾讯云对象存储的一些示例说明
具体以腾讯云对象存储SDK文档 (opens new window)为准
腾讯云SDK-PHP版本简单示例:
$tmpSecretId = "tmpSecretId"; //"临时密钥 SecretId",上面接口返回的【tmpSecretId】字段
$tmpSecretKey = "tmpSecretKey"; //"临时密钥 SecretKey",上面接口返回的【tmpSecretKey】字段
$tmpToken = "stsToken"; //"临时密钥 token",上面接口返回的【stsToken】字段
$region = "region"; //设置一个默认的存储桶地域,上面接口返回的【region】字段
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $tmpSecretId,
'secretKey' => $tmpSecretKey,
'token' => $tmpToken)));
### 上传文件流
try {
$bucket = "yach-1256123776"; //存储桶名称 格式:BucketName-APPID,上面接口返回的【bucket】字段
$key = "/dev/openApi/12345678/企业文化.docx"; //远程文件绝对路径,上面接口返回的【key】字段
$srcPath = "path/to/localFile/企业文化.docx"; //本地文件绝对路径
$file = fopen($srcPath, "rb");
if ($file) {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => $file));
print_r($result);
}
} catch (\Exception $e) {
echo "$e\n";
}
腾讯云SDK-Python版本简单示例:
# -*- coding=utf-8
# appid 已在配置中移除,请在参数 Bucket 中带上 appid。Bucket 由 BucketName-APPID 组成
# 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = 'tmpSecretId' # 替换为用户的 secretId,上面接口返回的【tmpSecretId】字段
secret_key = 'tmpSecretKey' # 替换为用户的 secretKey,上面接口返回的【tmpSecretKey】字段
region = 'region' # 替换为用户的 Region,上面接口返回的【region】字段
token = 'stsToken' # 使用临时密钥需要传入 Token,默认为空,可不填,上面接口返回的【stsToken】字段
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
# 2. 获取客户端对象
client = CosS3Client(config)
#### 文件流简单上传
# 强烈建议您以二进制模式(binary mode)打开文件,否则可能会导致错误
with open('path/to/localFile/企业文化.docx', 'rb') as fp:
response = client.put_object(
Bucket='yach-1256123776',
Body=fp,
Key='/dev/openApi/12345678/企业文化.docx',
StorageClass='STANDARD',
EnableMD5=False
)
print(response['ETag'])
腾讯云SDK-Java版本简单示例:
// 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey, sessionToken)
String tmpSecretId = "tmpSecretId"; //上面接口返回的【tmpSecretId】字段
String tmpSecretKey = "tmpSecretKey"; //上面接口返回的【tmpSecretKey】字段
String sessionToken = "stsToken"; //上面接口返回的【stsToken】字段
BasicSessionCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
// 2 设置 bucket 的区域, COS 地域的简称请参阅 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参阅源码或者常见问题 Java SDK 部分
Region region = new Region("region"); //上面接口返回的【region】字段
ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端
COSClient cosClient = new COSClient(cred, clientConfig);
// 指定要上传的文件
File localFile = new File(localFilePath);
// 指定要上传到的存储桶
String bucketName = "yach-1256123776";
// 指定要上传到 COS 上对象键
String key = "/dev/openApi/12345678/企业文化.docx";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
腾讯云SDK-Go版本简单示例:
// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
// 2.临时密钥
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: "tmpSecretId", //上面接口返回的【tmpSecretId】字段
SecretKey: "tmpSecretKey", //上面接口返回的【tmpSecretKey】字段
SessionToken: "stsToken", //上面接口返回的【stsToken】字段
},
})
if client != nil {
// 调用 COS 请求
}
package main
import (
"context"
"net/http"
"net/url"
"os"
"strings"
"github.com/tencentyun/cos-go-sdk-v5"
)
func main() {
// 将 examplebucket-1250000000 和 COS_REGION 修改为真实的信息
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: "tmpSecretId",
SecretKey: "tmpSecretKey",
},
})
// 对象键(Key)是对象在存储桶中的唯一标识。
// 例如,在对象的访问域名 `examplebucket-1250000000.cos.COS_REGION.myqcloud.com/test/objectPut.go` 中,对象键为 test/objectPut.go
name := "test/objectPut.go"
// 1.通过字符串上传对象
f := strings.NewReader("test")
_, err := c.Object.Put(context.Background(), name, f, nil)
if err != nil {
panic(err)
}
// 2.通过本地文件上传对象
_, err = c.Object.PutFromFile(context.Background(), name, "../test", nil)
if err != nil {
panic(err)
}
}