Skip to content

Commit 1fc9e52

Browse files
committed
fix event, cors api, supply their types and more reliable tests
1 parent c4c9b2e commit 1fc9e52

File tree

4 files changed

+206
-73
lines changed

4 files changed

+206
-73
lines changed

index.d.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,18 @@ export declare namespace rs {
578578
delimiter?: string;
579579
}
580580

581+
type BucketEventName = 'put'
582+
| 'mkfile'
583+
| 'delete'
584+
| 'copy'
585+
| 'move'
586+
| 'append'
587+
| 'disable'
588+
| 'enable'
589+
| 'deleteMarkerCreate'
590+
| 'predelete'
591+
| 'restore:completed';
592+
581593
class BucketManager {
582594
mac: auth.digest.Mac;
583595
config: conf.Config;
@@ -866,6 +878,113 @@ export declare namespace rs {
866878
* @param callbackFunc - 回调函数
867879
*/
868880
getBucketLifecycleRule(bucket: string, callbackFunc: callback): void
881+
882+
/**
883+
* 添加事件通知
884+
* https://developer.qiniu.com/kodo/8610/dev-event-notification
885+
* @param bucket - 空间名
886+
* @param options - 配置项
887+
* @param options.name - 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
888+
* @param options.event - 事件类型,接受数组设置多个
889+
* @param options.callbackUrl - 事件通知回调 URL,接受数组设置多个,失败依次重试
890+
* @param options.prefix - 可选,文件配置的前缀
891+
* @param options.suffix - 可选,文件配置的后缀
892+
* @param options.access_key - 可选,设置的话会对通知请求用对应的ak、sk进行签名
893+
* @param options.host - 可选,通知请求的host
894+
* @param callbackFunc - 回调函数
895+
*/
896+
putBucketEvent(
897+
bucket: string,
898+
options: {
899+
name: string,
900+
event: BucketEventName | BucketEventName[],
901+
callbackUrl: string | string[],
902+
prefix?: string,
903+
suffix?: string,
904+
access_key?: string,
905+
host?: string,
906+
},
907+
callbackFunc: callback,
908+
): void
909+
910+
/**
911+
* 更新事件通知
912+
* https://developer.qiniu.com/kodo/8610/dev-event-notification
913+
* @param bucket - 空间名
914+
* @param options - 配置项
915+
* @param options.name - 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
916+
* @param options.event - 事件类型,接受数组设置多个
917+
* @param options.callbackUrl - 事件通知回调 URL,接受数组设置多个,失败依次重试
918+
* @param options.prefix - 可选,文件配置的前缀
919+
* @param options.suffix - 可选,文件配置的后缀
920+
* @param options.access_key - 可选,设置的话会对通知请求用对应的ak、sk进行签名
921+
* @param options.host - 可选,通知请求的host
922+
* @param callbackFunc - 回调函数
923+
*/
924+
updateBucketEvent(
925+
bucket: string,
926+
options: {
927+
name: string,
928+
event?: BucketEventName | BucketEventName[],
929+
callbackUrl?: string | string[],
930+
prefix?: string,
931+
suffix?: string,
932+
access_key?: string,
933+
host?: string,
934+
},
935+
callbackFunc: callback,
936+
): void
937+
938+
/**
939+
* 获取事件通知规则
940+
* https://developer.qiniu.com/kodo/8610/dev-event-notification
941+
*
942+
* @param bucket - 空间名
943+
* @param callbackFunc - 回调函数
944+
*/
945+
getBucketEvent(bucket: string, callbackFunc: callback): void
946+
947+
/**
948+
* 删除事件通知规则
949+
* https://developer.qiniu.com/kodo/8610/dev-event-notification
950+
*
951+
* @param bucket - 空间名
952+
* @param name - 规则名称
953+
* @param callbackFunc - 回调函数
954+
*/
955+
deleteBucketEvent(bucket: string, name: string, callbackFunc: callback): void
956+
957+
/**
958+
* 设置 bucket 的 cors(跨域)规则
959+
* https://developer.qiniu.com/kodo/8539/set-the-cross-domain-resource-sharing
960+
* @param bucket - 空间名
961+
* @param body - 规则配置
962+
* @param body[].allowed_origin - 允许的域名
963+
* @param body[].allowed_method - 允许的请求方法;大小写不敏感
964+
* @param body[].allowed_header - 可选,允许的 header;默认不允许任何 header;大小写不敏感
965+
* @param body[].exposed_header - 可选,暴露的 header;默认 X-Log, X-Reqid;大小写不敏感
966+
* @param body[].max_age - 可选,结果可以缓存的时间;默认不缓存
967+
* @param callbackFunc - 回调函数
968+
*/
969+
putCorsRules(
970+
bucket: string,
971+
body: {
972+
allowed_origin: string[],
973+
allowed_method: string[],
974+
allowed_header?: string[],
975+
exposed_header?: string[],
976+
max_age?: number,
977+
}[],
978+
callbackFunc: callback
979+
): void
980+
981+
/**
982+
* 获取 bucket 的 cors(跨域)规则
983+
* https://developer.qiniu.com/kodo/8539/set-the-cross-domain-resource-sharing
984+
* @param bucket - 空间名
985+
* @param callbackFunc - 回调函数
986+
*/
987+
getCorsRules(bucket: string, callbackFunc: callback): void
869988
}
870989

871990
/**

qiniu/storage/resume.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ function resumeUploadV2 (
474474
blkStream.pause();
475475
partNumber = finishedBlock + 1;
476476
const bodyMd5 = util.getMd5(chunk);
477-
uploadPart(bucket, upDomain, uploadToken, encodedObjectName, chunk, finishedEtags.uploadId, partNumber,
477+
uploadPart(bucket, upDomain, uploadToken, encodedObjectName, chunk, finishedEtags.uploadId, partNumber, putExtra,
478478
function (respErr, respBody, respInfo) {
479479
if (respInfo.statusCode !== 200 || respBody.md5 !== bodyMd5) {
480480
callbackFunc(respErr, respBody, respInfo);
@@ -515,15 +515,26 @@ function resumeUploadV2 (
515515
});
516516
}
517517

518-
function uploadPart (bucket, upDomain, uploadToken, encodedObjectName, chunk, uploadId, partNumber, callbackFunc) {
518+
function uploadPart (bucket, upDomain, uploadToken, encodedObjectName, chunk, uploadId, partNumber, putExtra, callbackFunc) {
519519
const headers = {
520520
Authorization: 'UpToken ' + uploadToken,
521521
'Content-Type': 'application/octet-stream',
522522
'Content-MD5': util.getMd5(chunk)
523523
};
524524
const requestUrl = upDomain + '/buckets/' + bucket + '/objects/' + encodedObjectName + '/uploads/' + uploadId +
525525
'/' + partNumber.toString();
526-
rpc.put(requestUrl, chunk, headers, callbackFunc);
526+
rpc.put(requestUrl, chunk, headers, function (err, ret, info) {
527+
if (info.statusCode === 612) {
528+
if (putExtra.resumeRecordFile) {
529+
try {
530+
fs.unlinkSync(putExtra.resumeRecordFile);
531+
} catch (_e) {
532+
// ignore
533+
}
534+
}
535+
}
536+
callbackFunc(err, ret, info);
537+
});
527538
}
528539

529540
function completeParts (upDomain, bucket, encodedObjectName, uploadToken, finishedEtags,

qiniu/storage/rs.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ function PutBucketEvent(mac, config, options, bucket, callbackFunc) {
962962
requestURI,
963963
null,
964964
{
965-
mac: this.mac
965+
mac: mac
966966
},
967967
callbackFunc
968968
);
@@ -1011,7 +1011,7 @@ function UpdateBucketEvent(mac, config, options, bucket, callbackFunc) {
10111011
requestURI,
10121012
null,
10131013
{
1014-
mac: this.mac
1014+
mac: mac
10151015
},
10161016
callbackFunc
10171017
);
@@ -1098,7 +1098,7 @@ function PutReferAntiLeech(mac, config, bucket, options, callbackFunc) {
10981098
requestURI,
10991099
null,
11001100
{
1101-
mac: this.mac
1101+
mac: mac
11021102
},
11031103
callbackFunc
11041104
);
@@ -1115,9 +1115,9 @@ function PutCorsRules(mac, config, bucket, body, callbackFunc) {
11151115
var requestURI = scheme + conf.UC_HOST + '/corsRules/set/' + bucket;
11161116
rpc.postWithOptions(
11171117
requestURI,
1118-
null,
1118+
reqBody,
11191119
{
1120-
mac: this.mac
1120+
mac: mac
11211121
},
11221122
callbackFunc
11231123
);
@@ -1135,7 +1135,7 @@ function GetCorsRules(mac, config, bucket, callbackFunc) {
11351135
requestURI,
11361136
null,
11371137
{
1138-
mac: this.mac
1138+
mac: mac
11391139
},
11401140
callbackFunc
11411141
);

0 commit comments

Comments
 (0)