Skip to content

Commit

Permalink
去除两个一定跨域的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Sep 1, 2017
1 parent 1eee838 commit 18c7825
Show file tree
Hide file tree
Showing 6 changed files with 664 additions and 537 deletions.
21 changes: 2 additions & 19 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ console.error = function (text) {
logger(text, 'red');
};

function getService() {
cos.getService(function (err, data) {
console.log(err || data);
});
}

function getAuth() {
var AppId = config.AppId;
var Bucket = config.Bucket;
Expand All @@ -93,16 +87,7 @@ function getAuth() {
method: 'get',
pathname: '/' + key
}, function (auth) {
console.log('http://' + Bucket + '-' + AppId + '.' + config.Region + '.myqcloud.com' + '/' + key + '?sign=' + encodeURIComponent(auth));
});
}

function putBucket() {
cos.putBucket({
Bucket: 'testnew',
Region: config.Region
}, function (err, data) {
console.log(err || data);
console.log('http://' + Bucket + '-' + AppId + '.cos.' + config.Region + '.myqcloud.com' + '/' + key + '?sign=' + encodeURIComponent(auth));
});
}

Expand Down Expand Up @@ -378,7 +363,7 @@ function putObjectCopy() {
Bucket: config.Bucket,
Region: config.Region,
Key: '1mb.copy.zip',
CopySource: Bucket + '-' + AppId + '.' + config.Region + '.myqcloud.com/1mb.zip',
CopySource: Bucket + '-' + AppId + '.cos.' + config.Region + '.myqcloud.com/1mb.zip',
}, function (err, data) {
console.log(err || data);
});
Expand Down Expand Up @@ -562,9 +547,7 @@ function restartTask() {
console.log('restart');
}

// getService();
// getAuth();
// putBucket();
// getBucket();
// headBucket();
// putBucketAcl();
Expand Down
105 changes: 8 additions & 97 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15397,49 +15397,6 @@ var util = __webpack_require__(16);

// Bucket 相关

/**
* 获取用户的 bucket 列表
* @param {Object} params 回调函数,必须,下面为参数列表
* 无特殊参数
* @param {Function} callback 回调函数,必须
*/
function getService(params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';
var domain = this.options.ServiceDomain;
var appId = params.AppId || this.options.appId;
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId || '').replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
url: domain,
method: 'GET'
}, function (err, data) {
if (err) {
return callback(err);
}
var buckets = data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Buckets && data.ListAllMyBucketsResult.Buckets.Bucket || [];
buckets = util.isArray(buckets) ? buckets : [buckets];
callback(null, {
Buckets: buckets,
statusCode: data.statusCode,
headers: data.headers
});
});
}

/**
* 查看是否存在该Bucket,是否有权限访问
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -15538,52 +15495,6 @@ function getBucket(params, callback) {
});
}

/**
* 创建 Bucket,并初始化访问权限
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.ACL 用户自定义文件权限,可以设置:private,public-read;默认值:private,非必须
* @param {String} params.GrantRead 赋予被授权者读的权限,格式x-cos-grant-read: uin=" ",uin=" ",非必须
* @param {String} params.GrantWrite 赋予被授权者写的权限,格式x-cos-grant-write: uin=" ",uin=" ",非必须
* @param {String} params.GrantFullControl 赋予被授权者读写权限,格式x-cos-grant-full-control: uin=" ",uin=" ",非必须
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。
* @return {Object} data 返回的数据
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {
var self = this;
var headers = {};
headers['x-cos-acl'] = params['ACL'];
headers['x-cos-grant-read'] = params['GrantRead'];
headers['x-cos-grant-write'] = params['GrantWrite'];
headers['x-cos-grant-full-control'] = params['GrantFullControl'];
var appId = this.options.AppId || '';
submitRequest.call(this, {
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
AppId: params.AppId,
headers: headers
}, function (err, data) {
if (err) {
return callback(err);
}
var url = getUrl({
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
appId: appId
});
callback(null, {
Location: url,
statusCode: data.statusCode,
headers: data.headers
});
});
}

/**
* 删除 Bucket
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -16279,8 +16190,8 @@ function _putObject(params, callback) {
var Body = params.Body;
var readStream;

if (util.isBrowser && (Body instanceof global.Blob || Body instanceof global.File)) {
// 传入 Blob 或者 File 文件内容
if (util.isBrowser && (typeof Body === 'string' || Body instanceof global.Blob || Body instanceof global.File)) {
// 在浏览器传入 String、Blob 或者 File 文件内容
headers['Content-Length'] = Body.length;
} else if (Body && Body instanceof Buffer) {
// 传入 fs.readFileSync(filepath) 或者 文件内容
Expand Down Expand Up @@ -17205,10 +17116,8 @@ function submitRequest(params, callback) {

var API_MAP = {
// Bucket 相关方法
getService: getService,
getBucket: getBucket,
headBucket: headBucket,
putBucket: putBucket,
deleteBucket: deleteBucket,
getBucketAcl: getBucketAcl,
putBucketAcl: putBucketAcl,
Expand Down Expand Up @@ -22508,10 +22417,12 @@ function abortUploadTaskArray(params, callback) {

for (var i = 0, len = datas.length; i < len; i++) {
var item = datas[i];
if (item['error']) {
errorList.push(item['task']);
} else {
successList.push(item['task']);
if (item['task']) {
if (item['error']) {
errorList.push(item['task']);
} else {
successList.push(item['task']);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,12 @@ function abortUploadTaskArray(params, callback) {

for (var i = 0, len = datas.length; i < len; i++) {
var item = datas[i];
if (item['error']) {
errorList.push(item['task']);
} else {
successList.push(item['task']);
if (item['task']) {
if (item['error']) {
errorList.push(item['task']);
} else {
successList.push(item['task']);
}
}
}

Expand Down
94 changes: 1 addition & 93 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,6 @@ var util = require('./util');

// Bucket 相关

/**
* 获取用户的 bucket 列表
* @param {Object} params 回调函数,必须,下面为参数列表
* 无特殊参数
* @param {Function} callback 回调函数,必须
*/
function getService(params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';
var domain = this.options.ServiceDomain;
var appId = params.AppId || this.options.appId;
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId || '').replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
url: domain,
method: 'GET',
}, function (err, data) {
if (err) {
return callback(err);
}
var buckets = (data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Buckets
&& data.ListAllMyBucketsResult.Buckets.Bucket) || [];
buckets = util.isArray(buckets) ? buckets : [buckets];
callback(null, {
Buckets: buckets,
statusCode: data.statusCode,
headers: data.headers,
});
});
}

/**
* 查看是否存在该Bucket,是否有权限访问
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -147,52 +103,6 @@ function getBucket(params, callback) {
});
}

/**
* 创建 Bucket,并初始化访问权限
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.ACL 用户自定义文件权限,可以设置:private,public-read;默认值:private,非必须
* @param {String} params.GrantRead 赋予被授权者读的权限,格式x-cos-grant-read: uin=" ",uin=" ",非必须
* @param {String} params.GrantWrite 赋予被授权者写的权限,格式x-cos-grant-write: uin=" ",uin=" ",非必须
* @param {String} params.GrantFullControl 赋予被授权者读写权限,格式x-cos-grant-full-control: uin=" ",uin=" ",非必须
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。
* @return {Object} data 返回的数据
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {
var self = this;
var headers = {};
headers['x-cos-acl'] = params['ACL'];
headers['x-cos-grant-read'] = params['GrantRead'];
headers['x-cos-grant-write'] = params['GrantWrite'];
headers['x-cos-grant-full-control'] = params['GrantFullControl'];
var appId = this.options.AppId || '';
submitRequest.call(this, {
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
AppId: params.AppId,
headers: headers,
}, function (err, data) {
if (err) {
return callback(err);
}
var url = getUrl({
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
appId: appId
});
callback(null, {
Location: url,
statusCode: data.statusCode,
headers: data.headers,
});
});
}

/**
* 删除 Bucket
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -893,7 +803,7 @@ function _putObject(params, callback) {
var Body = params.Body;
var readStream;

if (util.isBrowser && (Body instanceof global.Blob || Body instanceof global.File)) { // 传入 Blob 或者 File 文件内容
if (util.isBrowser && (typeof Body === 'string' || Body instanceof global.Blob || Body instanceof global.File)) { // 在浏览器传入 String、Blob 或者 File 文件内容
headers['Content-Length'] = Body.length;
} else if (Body && Body instanceof Buffer) { // 传入 fs.readFileSync(filepath) 或者 文件内容
headers['Content-Length'] = Body.length;
Expand Down Expand Up @@ -1824,10 +1734,8 @@ function submitRequest(params, callback) {

var API_MAP = {
// Bucket 相关方法
getService: getService,
getBucket: getBucket,
headBucket: headBucket,
putBucket: putBucket,
deleteBucket: deleteBucket,
getBucketAcl: getBucketAcl,
putBucketAcl: putBucketAcl,
Expand Down
Loading

0 comments on commit 18c7825

Please sign in to comment.