Skip to content

Commit

Permalink
fix upload
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jul 9, 2017
1 parent 3658086 commit 915ea79
Show file tree
Hide file tree
Showing 11 changed files with 29,247 additions and 54 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

## get started

1. 使用 SDK 需要浏览器支持HTML 5
2. 请您到https://console.qcloud.com/cos 获取您的 AppId、Bucket、Region、SecretId 和 SecretKey
3. 请您到https://console.qcloud.com/cos 针对您要操作的bucket进行跨域(CORS)设置,可以按照如下范例,修改允许的来源 Origin 和 Headers:

![cors.png](./demo/cors.png)

```html
<script src="dist/cos-js-sdk-v5.js"></script>
<script>
Expand Down
Binary file added demo/cors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 30 additions & 29 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
var getAuthorization = function (options, callback) {

// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/';
var url = '../server/auth.php?method=' + method + '&pathname=' + pathname;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
callback(e.target.responseText);
};
xhr.send();

// // 方法二(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
// });
// callback(authorization);

};

var cos = new COS({
AppId: config.AppId,
getAuthorization: function (options, callback) {

// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/';
var url = '../server/auth.php?method=' + method + '&pathname=' + pathname;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
callback(e.target.responseText);
};
xhr.send();

// // 方法二(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
// });
// callback(authorization);

}
ChunkParallelLimit: 1,
getAuthorization: getAuthorization
});

var pre = document.querySelector('.result');
Expand Down Expand Up @@ -66,7 +69,7 @@ function getAuth() {
AppId = arr[1];
}
var key = '1.png';
cos.getAuthorization({
getAuthorization({
method: 'get',
pathname: '/' + key
}, function (auth) {
Expand Down Expand Up @@ -314,7 +317,7 @@ function deleteBucket() {
function putObject() {
// 创建测试文件
var filename = '1mb.zip';
var blob = util.createFile({type: 'image/png', size: 1024 * 1024});
var blob = util.createFile({size: 1024 * 1024});
// 调用方法
cos.putObject({
Bucket: config.Bucket, /* 必须 */
Expand Down Expand Up @@ -446,13 +449,11 @@ function abortUploadTask() {
}

function sliceUploadFile() {
var blob = util.createFile({type: 'image/png', size: 1024 * 1024 * 10});
var blob = util.createFile({size: 1024 * 1024 * 10});
cos.sliceUploadFile({
Bucket: config.Bucket, /* 必须 */
Region: config.Region,
Key: '10mb.zip', /* 必须 */
SliceSize: 1024 * 1024, //1MB /* 非必须 */
AsyncLimit: 3, /* 非必须 */
Body: blob,
onHashProgress: function (progressData) {
console.log(JSON.stringify(progressData));
Expand All @@ -465,7 +466,7 @@ function sliceUploadFile() {
});
}

getService();
// getService();
// getAuth();
// putBucket();
// getBucket();
Expand Down
4 changes: 3 additions & 1 deletion demo/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var util = {
arr.forEach(function (char, i) {
arr[i] = 0;
});
var blob = new Blob([buffer], {type: options.type || 'image/png'});
var opt = {};
options.type && (opt.type = options.type);
var blob = new Blob([buffer], options);
return blob;
}
};
29,181 changes: 29,175 additions & 6 deletions dist/cos-js-sdk-v5.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
"dev": "webpack -w",
"build": "webpack"
"dev": "SET NODE_ENV=development&& webpack -w",
"build": "SET NODE_ENV=development&& webpack"
},
"repository": {
"type": "git",
Expand Down
8 changes: 2 additions & 6 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function _sliceUploadFile (params, callback) {
Region: Region,
Key: Key,
StorageClass: StorageClass,
FilePath: FilePath,
Body: Body,
FileSize: FileSize,
SliceSize: SliceSize,
onHashProgress: onHashProgress,
Expand Down Expand Up @@ -541,11 +541,7 @@ function uploadSliceItem(params, callback) {
ContentLength = end - start;
}

var Body = fs.createReadStream(FilePath, {
start: start,
end: end - 1
});

var Body = util.fileSlice.call(FileBody, start, end);
var ContentSha1 = UploadData.PartList[PartNumber - 1].ETag;
Async.retry(sliceRetryTimes, function (tryCallback) {
if (!self._isRunningTask(TaskId)) return;
Expand Down
4 changes: 2 additions & 2 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,6 @@ function _putObject(params, callback) {
Key: params.Key,
headers: headers,
body: Body,
inputStream: readStream,
onProgress: onFileProgress
}, function (err, data) {
params.TaskId === TaskId && self.off('inner-kill-task', killTask);
Expand Down Expand Up @@ -1375,7 +1374,7 @@ function multipartUpload(params, callback) {
Key: params.Key,
action: action,
headers: headers,
inputStream: params.Body || null,
body: params.Body || null,
onProgress: params.onProgress
}, function (err, data) {
if (params.TaskId) self.off('inner-kill-task', killTask);
Expand Down Expand Up @@ -1814,6 +1813,7 @@ var API_MAP = {
getObject: getObject,
headObject: headObject,
putObject: putObject,
_putObject: _putObject,
deleteObject: deleteObject,
getObjectAcl: getObjectAcl,
putObjectAcl: putObjectAcl,
Expand Down
9 changes: 4 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,9 @@ function clone(obj) {
});
}
function extend(target, source) {
for (var method in source) {
if (!target[method]) {
target[method] = source[method];
}
}
each(source, function (val, key) {
target[key] = source[key];
});
return target;
}
function isArray(arr) {
Expand Down Expand Up @@ -279,6 +277,7 @@ var apiWrapper = function (apiName, apiFn) {
var fileSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;

var util = {
fileSlice: fileSlice,
apiWrapper: apiWrapper,
getAuth: getAuth,
xml2json: xml2json,
Expand Down
15 changes: 12 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ module.exports = {
performance: {
hints: false
},
plugins: [
};

if (process.env.NODE_ENV === 'production') {
module.exports.output.filename = 'cos-js-sdk-v5.min.js';
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
Expand All @@ -33,5 +42,5 @@ module.exports = {
new webpack.LoaderOptionsPlugin({
minimize: true
})
]
};
])
}

0 comments on commit 915ea79

Please sign in to comment.