Skip to content

Commit 9737bac

Browse files
authored
Merge pull request #129 from livehigh/feat/1.3.0
Feat/1.3.0
2 parents bd876c1 + 07d9134 commit 9737bac

File tree

10 files changed

+395
-15
lines changed

10 files changed

+395
-15
lines changed

demo/demo.js

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @ts-check
2+
// config 替换成自己的存储桶和账号信息
23
var config = {
34
Bucket: 'test-1250000000',
4-
Region: 'ap-guangzhou'
5+
Region: 'ap-guangzhou',
6+
Uin: '10001',
57
};
68

79
var util = {
@@ -803,7 +805,7 @@ function putBucketInventory() {
803805
COSBucketDestination: {
804806
Format: 'CSV',
805807
AccountId: config.Uin,
806-
Bucket: 'qcs::cos:ap-guangzhou::bucket-logging-' + AppId,
808+
Bucket: 'qcs::cos:' + config.Region + '::' + config.Bucket,
807809
Prefix: 'inventory',
808810
Encryption: {
809811
SSECOS: ''
@@ -961,6 +963,43 @@ function putObject_base64ToBlob() {
961963
});
962964
}
963965

966+
function appendObject() {
967+
cos.appendObject({
968+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
969+
Region: config.Region,
970+
Key: 'append.txt', /* 必须 */
971+
Body: '12345',
972+
Position: 0,
973+
},
974+
function(err, data) {
975+
logger.log('putObject:', err || data);
976+
})
977+
}
978+
979+
function appendObject_continue() {
980+
cos.headObject({
981+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
982+
Region: config.Region,
983+
Key: 'append.txt', /* 必须 */
984+
}, function(err, data) {
985+
if (err) return console.log(err);
986+
// 首先取到要追加的文件当前长度,即需要上送的Position
987+
var position = data.headers['content-length'];
988+
cos.appendObject({
989+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
990+
Region: config.Region,
991+
Key: 'append.txt', /* 必须 */
992+
Body: '66666',
993+
Position: position,
994+
},
995+
function(err, data) {
996+
// 也可以取到下一次上传的position继续追加上传
997+
// var nextPosition = data.headers['x-cos-next-append-position'];
998+
logger.log('putObject:', err || data);
999+
})
1000+
});
1001+
}
1002+
9641003
function putObjectCopy() {
9651004
cos.putObjectCopy({
9661005
Bucket: config.Bucket, // Bucket 格式:test-1250000000
@@ -1640,6 +1679,67 @@ function CIExample4(){
16401679
);
16411680
}
16421681

1682+
// 查询已经开通数据万象功能的存储桶
1683+
function describeMediaBuckets() {
1684+
var host = 'ci.' + config.Region + '.myqcloud.com';
1685+
var url = 'https://' + host + '/mediabucket';
1686+
cos.request({
1687+
Bucket: config.Bucket,
1688+
Region: config.Region,
1689+
Method: 'GET',
1690+
Key: 'mediabucket', /** 固定值,必须 */
1691+
Url: url,
1692+
Query: {
1693+
pageNumber: '1', /** 第几页,非必须 */
1694+
pageSize: '10', /** 每页个数,非必须 */
1695+
// regions: 'ap-chengdu', /** 地域信息,例如'ap-beijing',支持多个值用逗号分隔如'ap-shanghai,ap-beijing',非必须 */
1696+
// bucketNames: 'test-1250000000', /** 存储桶名称,精确搜索,例如'test-1250000000',支持多个值用逗号分隔如'test1-1250000000,test2-1250000000',非必须 */
1697+
// bucketName: 'test', /** 存储桶名称前缀,前缀搜索,例如'test',支持多个值用逗号分隔如'test1,test2',非必须 */
1698+
}
1699+
}, function (err, data) {
1700+
logger.log(err || data);
1701+
});
1702+
}
1703+
1704+
1705+
// 获取媒体文件信息
1706+
function getMediaInfo() {
1707+
cos.request({
1708+
Bucket: config.Bucket,
1709+
Region: config.Region,
1710+
Method: 'GET',
1711+
Key: 'test.mp4',
1712+
Query: {
1713+
'ci-process': 'videoinfo' /** 固定值,必须 */
1714+
}
1715+
}, function (err, data) {
1716+
logger.log(err || data);
1717+
});
1718+
}
1719+
1720+
// 获取媒体文件某个时间的截图
1721+
function getSnapshot() {
1722+
cos.request({
1723+
Bucket: config.Bucket,
1724+
Region: config.Region,
1725+
Method: 'GET',
1726+
Key: 'test.mp4',
1727+
Query: {
1728+
'ci-process': 'snapshot', /** 固定值,必须 */
1729+
time: 1, /** 截图的时间点,单位为秒,必须 */
1730+
// width: 0, /** 截图的宽,非必须 */
1731+
// height: 0, /** 截图的高,非必须 */
1732+
// format: 'jpg', /** 截图的格式,支持 jpg 和 png,默认 jpg,非必须 */
1733+
// rotate: 'auto', /** 图片旋转方式,默认为'auto',非必须 */
1734+
// mode: 'exactframe', /** 截帧方式,默认为'exactframe',非必须 */
1735+
},
1736+
RawBody: true,
1737+
},
1738+
function(err, data){
1739+
logger.log(err || data);
1740+
});
1741+
}
1742+
16431743
(function () {
16441744
var list = [
16451745
'header-工具函数',
@@ -1706,6 +1806,8 @@ function CIExample4(){
17061806
'selectObjectContent',
17071807
'putObject',
17081808
'putObject_base64ToBlob',
1809+
'appendObject',
1810+
'appendObject_continue',
17091811

17101812
'header-高级操作',
17111813
'uploadFile',
@@ -1728,10 +1830,15 @@ function CIExample4(){
17281830
'CIExample2',
17291831
'CIExample3',
17301832
'CIExample4',
1833+
'describeMediaBuckets',
1834+
'getMediaInfo',
1835+
'getSnapshot',
17311836
];
17321837
var labelMap = {
17331838
putObject: '简单上传',
17341839
putObject_base64ToBlob: '简单上传:base64转blob',
1840+
appendObject: '追加上传',
1841+
appendObject_continue: '查询position并追加上传',
17351842
uploadFile: '高级上传',
17361843
sliceUploadFile: '分片上传',
17371844
sliceCopyFile: '分片复制',
@@ -1746,6 +1853,9 @@ function CIExample4(){
17461853
CIExample2: '对云上数据进行图片处理',
17471854
CIExample3: '下载时使用图片处理',
17481855
CIExample4: '生成带图片处理参数的签名 URL',
1856+
describeMediaBuckets: '查询媒体处理开通情况',
1857+
getMediaInfo: '获取媒体文件信息',
1858+
getSnapshot: '获取媒体文件某个时间的截图',
17491859
};
17501860
var container = document.querySelector('.main');
17511861
var html = [];

dist/cos-js-sdk-v5.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ var formatParams = function (apiName, params) {
531531
'x-cos-grant-write-acp': 'GrantWriteAcp',
532532
'x-cos-storage-class': 'StorageClass',
533533
'x-cos-traffic-limit': 'TrafficLimit',
534+
'x-cos-mime-limit': 'MimeLimit',
534535
// SSE-C
535536
'x-cos-server-side-encryption-customer-algorithm': 'SSECustomerAlgorithm',
536537
'x-cos-server-side-encryption-customer-key': 'SSECustomerKey',
@@ -740,6 +741,13 @@ var isNode = function () {
740741
return typeof window !== 'object' && typeof process === 'object' && "function" === 'function';
741742
};
742743

744+
var isCIHost = function (url) {
745+
if (url && url.split('?')[0].match(/(.ci.|ci.|.ci)/g)) {
746+
return true;
747+
}
748+
return false;
749+
};
750+
743751
var util = {
744752
noop: noop,
745753
formatParams: formatParams,
@@ -772,7 +780,8 @@ var util = {
772780
parseSelectPayload: parseSelectPayload,
773781
getSourceParams: getSourceParams,
774782
isBrowser: true,
775-
isNode: isNode
783+
isNode: isNode,
784+
isCIHost: isCIHost
776785
};
777786

778787
module.exports = util;
@@ -2441,7 +2450,7 @@ base.init(COS, task);
24412450
advance.init(COS, task);
24422451

24432452
COS.getAuthorization = util.getAuth;
2444-
COS.version = '1.2.20';
2453+
COS.version = '1.3.0';
24452454

24462455
module.exports = COS;
24472456

@@ -7712,7 +7721,9 @@ function request(params, callback) {
77127721
action: params.Action,
77137722
headers: params.Headers,
77147723
qs: params.Query,
7715-
body: params.Body
7724+
body: params.Body,
7725+
Url: params.Url,
7726+
rawBody: params.RawBody
77167727
}, function (err, data) {
77177728
if (err) return callback(err);
77187729
if (data && data.body) {
@@ -7723,6 +7734,57 @@ function request(params, callback) {
77237734
});
77247735
}
77257736

7737+
/**
7738+
* 追加上传
7739+
* @param {Object} params 参数对象,必须
7740+
* @param {String} params.Bucket Bucket名称,必须
7741+
* @param {String} params.Region 地域名称,必须
7742+
* @param {String} params.Key object名称,必须
7743+
* @param {File || Blob || String} params.Body 上传文件对象或字符串
7744+
* @param {Number} params.Position 追加操作的起始点,单位为字节,必须
7745+
* @param {String} params.CacheControl RFC 2616 中定义的缓存策略,将作为 Object 元数据保存,非必须
7746+
* @param {String} params.ContentDisposition RFC 2616 中定义的文件名称,将作为 Object 元数据保存,非必须
7747+
* @param {String} params.ContentEncoding RFC 2616 中定义的编码格式,将作为 Object 元数据保存,非必须
7748+
* @param {String} params.ContentLength RFC 2616 中定义的 HTTP 请求内容长度(字节),必须
7749+
* @param {String} params.ContentType RFC 2616 中定义的内容类型(MIME),将作为 Object 元数据保存,非必须
7750+
* @param {String} params.Expect 当使用 Expect: 100-continue 时,在收到服务端确认后,才会发送请求内容,非必须
7751+
* @param {String} params.Expires RFC 2616 中定义的过期时间,将作为 Object 元数据保存,非必须
7752+
* @param {String} params.ACL 允许用户自定义文件权限,有效值:private | public-read,非必须
7753+
* @param {String} params.GrantRead 赋予被授权者读取对象的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
7754+
* @param {String} params.GrantReadAcp 赋予被授权者读取对象的访问控制列表(ACL)的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
7755+
* @param {String} params.GrantWriteAcp 赋予被授权者写入对象的访问控制列表(ACL)的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
7756+
* @param {String} params.GrantFullControl 赋予被授权者操作对象的所有权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
7757+
* @param {String} params.StorageClass 设置对象的存储级别,枚举值:STANDARD、STANDARD_IA、ARCHIVE,默认值:STANDARD,非必须
7758+
* @param {String} params.x-cos-meta-* 允许用户自定义的头部信息,将作为对象的元数据保存。大小限制2KB,非必须
7759+
* @param {String} params.ContentSha1 RFC 3174 中定义的 160-bit 内容 SHA-1 算法校验,非必须
7760+
* @param {String} params.ServerSideEncryption 支持按照指定的加密算法进行服务端数据加密,格式 x-cos-server-side-encryption: "AES256",非必须
7761+
* @param {Function} callback 回调函数,必须
7762+
* @return {Object} err 请求失败的错误,如果请求成功,则为空。https://cloud.tencent.com/document/product/436/7730
7763+
* @return {Object} data 返回的数据
7764+
*/
7765+
function appendObject(params, callback) {
7766+
// 特殊处理 Cache-Control、Content-Type,避免代理更改这两个字段导致写入到 Object 属性里
7767+
var headers = params.Headers;
7768+
if (!headers['Cache-Control'] && !headers['cache-control']) headers['Cache-Control'] = '';
7769+
if (!headers['Content-Type'] && !headers['content-type']) headers['Content-Type'] = params.Body && params.Body.type || '';
7770+
submitRequest.call(this, {
7771+
Action: 'name/cos:AppendObject',
7772+
method: 'POST',
7773+
Bucket: params.Bucket,
7774+
Region: params.Region,
7775+
action: 'append',
7776+
Key: params.Key,
7777+
body: params.Body,
7778+
qs: {
7779+
position: params.Position
7780+
},
7781+
headers: params.Headers
7782+
}, function (err, data) {
7783+
if (err) return callback(err);
7784+
callback(null, data);
7785+
});
7786+
}
7787+
77267788
/**
77277789
* 获取签名
77287790
* @param {Object} params 参数对象,必须
@@ -8189,7 +8251,8 @@ function submitRequest(params, callback) {
81898251
delete params.headers['token'];
81908252
delete params.headers['clientIP'];
81918253
delete params.headers['clientUA'];
8192-
delete params.headers['x-cos-security-token'];
8254+
params.headers['x-cos-security-token'] && delete params.headers['x-cos-security-token'];
8255+
params.headers['x-ci-security-token'] && delete params.headers['x-ci-security-token'];
81938256
}
81948257
next(tryTimes + 1);
81958258
} else {
@@ -8211,7 +8274,7 @@ function _submitRequest(params, callback) {
82118274
var region = params.Region;
82128275
var object = params.Key;
82138276
var method = params.method || 'GET';
8214-
var url = params.url;
8277+
var url = params.Url || params.url;
82158278
var body = params.body;
82168279
var rawBody = params.rawBody;
82178280

@@ -8246,12 +8309,18 @@ function _submitRequest(params, callback) {
82468309
body: body
82478310
};
82488311

8312+
// 兼容ci接口
8313+
var token = 'x-cos-security-token';
8314+
if (util.isCIHost(url)) {
8315+
token = 'x-ci-security-token';
8316+
}
8317+
82498318
// 获取签名
82508319
opt.headers.Authorization = params.AuthData.Authorization;
82518320
params.AuthData.Token && (opt.headers['token'] = params.AuthData.Token);
82528321
params.AuthData.ClientIP && (opt.headers['clientIP'] = params.AuthData.ClientIP);
82538322
params.AuthData.ClientUA && (opt.headers['clientUA'] = params.AuthData.ClientUA);
8254-
params.AuthData.SecurityToken && (opt.headers['x-cos-security-token'] = params.AuthData.SecurityToken);
8323+
params.AuthData.SecurityToken && (opt.headers[token] = params.AuthData.SecurityToken);
82558324

82568325
// 清理 undefined 和 null 字段
82578326
opt.headers && (opt.headers = util.clearKey(opt.headers));
@@ -8433,6 +8502,7 @@ var API_MAP = {
84338502
getObjectTagging: getObjectTagging,
84348503
deleteObjectTagging: deleteObjectTagging,
84358504
selectObjectContent: selectObjectContent,
8505+
appendObject: appendObject,
84368506

84378507
// 分块上传相关方法
84388508
uploadPartCopy: uploadPartCopy,

dist/cos-js-sdk-v5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)