cos-nodejs-sdk-v5
Advanced tools
Comparing version 2.9.15 to 2.9.17
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
201
demo/demo.js
// @ts-check | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('./util'); | ||
var COS = require('../index'); | ||
var util = require('./util'); | ||
var pathLib = require('path'); | ||
var config = require('./config'); | ||
var cos = new COS({ | ||
@@ -1266,8 +1266,43 @@ // 必选参数 | ||
/* 移动对象*/ | ||
function moveObject() { | ||
// COS 没有对象重命名或移动的接口,移动对象可以通过复制/删除对象实现 | ||
var source = 'source.txt'; | ||
var target = 'target.txt'; | ||
var copySource = config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + camSafeUrlEncode(source).replace(/%2F/g, '/'); | ||
cos.putObject({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: source, | ||
Body: 'hello!', | ||
}, function (err, data) { | ||
if (err) return logger.log(err); | ||
cos.putObjectCopy({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: target, | ||
CopySource: copySource, | ||
}, function (err, data) { | ||
if (err) return logger.log(err); | ||
cos.deleteObject({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: source, | ||
}, function (err, data) { | ||
logger.log(err || data); | ||
}); | ||
}); | ||
}); | ||
} | ||
/* 上传本地文件夹 */ | ||
function uploadFolder() { | ||
var localFolder = path.resolve(__dirname, '../test/'); | ||
var localFolder = '../test/'; | ||
var remotePrefix = 'folder/'; | ||
fs.readdir(localFolder, function (err, list) { | ||
util.fastListFolder(localFolder, function (err, list) { | ||
if (err) return console.error(err); | ||
var files = list.map(function (filename) { | ||
var files = list.map(function (file) { | ||
var filename = pathLib.relative(localFolder, file.path).replace(/\\/g, '/'); | ||
if (filename && file.isDir && !filename.endsWith('/')) | ||
filename += '/'; | ||
var Key = remotePrefix + filename; | ||
@@ -1278,3 +1313,3 @@ return { | ||
Key: Key, | ||
FilePath: path.resolve(localFolder, filename), | ||
FilePath: file.path, | ||
}; | ||
@@ -1299,2 +1334,15 @@ }); | ||
/* 创建文件夹 */ | ||
function createFolder() { | ||
cos.getBucket({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: 'folder/', // 对象存储没有实际的文件夹,可以创建一个路径以 / 结尾的空对象表示,能在部分场景中满足文件夹使用需要 | ||
Body: '', | ||
}, function(err, data) { | ||
console.log(err || data); | ||
}); | ||
} | ||
/* 列出文件夹下的文件 */ | ||
function listFolder() { | ||
@@ -1338,2 +1386,3 @@ var _listFolder = function(params, callback) { | ||
/* 删除指定文件夹下的所有对象(删除存储桶里指定前缀所有对象) */ | ||
function deleteFolder() { | ||
@@ -1392,2 +1441,3 @@ var _deleteFolder = function(params, callback) { | ||
/* 分片下载文件 */ | ||
function downloadFile() { | ||
@@ -1419,19 +1469,124 @@ // 单文件分片并发下载 | ||
function request() { | ||
// 对云上数据进行图片处理 | ||
var filename = 'exampleImage.png'; | ||
cos.request({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: filename, | ||
Method: 'POST', | ||
Action: 'image_process', | ||
Headers: { | ||
// 万象持久化接口,上传时持久化 | ||
'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}' | ||
}, | ||
}, function (err, data) { | ||
console.log(err || data); | ||
}); | ||
// 对云上数据进行图片处理 | ||
var filename = 'example_photo.png'; | ||
cos.request({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: filename, | ||
Method: 'POST', | ||
Action: 'image_process', | ||
Headers: { | ||
// 万象持久化接口,上传时持久化 | ||
'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "example_photo_ci_result.png", "rule": "imageMogr2/thumbnail/200x/"}]}' | ||
}, | ||
}, function (err, data) { | ||
console.log(err || data); | ||
}); | ||
} | ||
/** | ||
* function CIExample1 | ||
* @description 上传时使用图片处理 | ||
*/ | ||
function CIExample1(){ | ||
var filename = 'example_photo.png' | ||
var filepath = path.resolve(__dirname, filename); | ||
cos.putObject({ | ||
Bucket: config.Bucket, // Bucket 格式:test-1250000000 | ||
Region: config.Region, | ||
Key: filename, | ||
Body: fs.readFileSync(filepath), | ||
Headers: { | ||
// 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 100,宽度等比压缩 | ||
'Pic-Operations': | ||
'{"is_pic_info": 1, "rules": [{"fileid": "example_photo_ci_result.png", "rule": "imageMogr2/thumbnail/200x/"}]}', | ||
}, | ||
onTaskReady: function (tid) { | ||
TaskId = tid; | ||
}, | ||
onProgress: function (progressData) { | ||
console.log(JSON.stringify(progressData)); | ||
}, | ||
}, function (err, data) { | ||
console.log(err || data); | ||
}); | ||
} | ||
/** | ||
* function CIExample2 | ||
* @description 对云上数据进行图片处理 | ||
*/ | ||
function CIExample2(){ | ||
var filename = 'example_photo.png'; | ||
cos.request({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: filename, | ||
Method: 'POST', | ||
Action: 'image_process', | ||
Headers: { | ||
// 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩 | ||
'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "example_photo_ci_result.png", "rule": "imageMogr2/thumbnail/200x/"}]}' | ||
}, | ||
}, function (err, data) { | ||
console.log(err || data); | ||
}); | ||
} | ||
/** | ||
* function CIExample3 | ||
* @description 下载时使用图片处理 | ||
*/ | ||
function CIExample3(){ | ||
var filepath = path.resolve(__dirname, 'example_photo_ci_result.png'); | ||
cos.getObject({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: 'example_photo.png', | ||
QueryString: `imageMogr2/thumbnail/200x/`, | ||
}, | ||
function (err, data) { | ||
if(data){ | ||
fs.writeFileSync(filepath, data.Body); | ||
} else { | ||
console.log(err); | ||
} | ||
}, | ||
); | ||
} | ||
/** | ||
* function CIExample4 | ||
* @description 生成带图片处理参数的签名 URL | ||
*/ | ||
function CIExample4(){ | ||
// 生成带图片处理参数的文件签名URL,过期时间设置为 30 分钟。 | ||
cos.getObjectUrl({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: 'photo.png', | ||
QueryString: `imageMogr2/thumbnail/200x/`, | ||
Expires: 1800, | ||
Sign: true, | ||
}, | ||
function (err, data) { | ||
console.log(err || data); | ||
}, | ||
); | ||
// 生成带图片处理参数的文件URL,不带签名。 | ||
cos.getObjectUrl({ | ||
Bucket: config.Bucket, | ||
Region: config.Region, | ||
Key: 'photo.png', | ||
QueryString: `imageMogr2/thumbnail/200x/`, | ||
Sign: false, | ||
}, | ||
function (err, data) { | ||
console.log(err || data); | ||
}, | ||
); | ||
} | ||
// getService(); | ||
@@ -1511,3 +1666,7 @@ // getAuth(); | ||
// deleteObjectTagging(); | ||
// 其他示例 | ||
// moveObject(); | ||
// uploadFolder(); | ||
// createFolder(); | ||
// listFolder(); | ||
@@ -1514,0 +1673,0 @@ // deleteFolder(); |
107
demo/util.js
@@ -0,1 +1,4 @@ | ||
var Async = require('../sdk/async'); | ||
var Batch = require('batch'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
@@ -29,2 +32,106 @@ var fs = require('fs'); | ||
var simpleListFolder = function(rootPath, callback) { | ||
var result = []; | ||
var deep = function(dirPath, deepNext) { | ||
fs.readdir(dirPath, function (err, list) { | ||
if (err) return console.error(err); | ||
Async.eachLimit(list, 10, function (fileName, asyncNext) { | ||
var filePath = pathLib.resolve(dirPath, fileName); | ||
fs.stat(filePath, function (err, stats) { | ||
if (stats.isDirectory()) { | ||
result.push(filePath + pathLib.sep); | ||
deep(filePath, asyncNext); | ||
} else { | ||
result.push(filePath); | ||
asyncNext(); | ||
} | ||
}); | ||
}, deepNext); | ||
}); | ||
}; | ||
deep(rootPath, function (err) { | ||
result.sort(); | ||
callback(err, result); | ||
}); | ||
}; | ||
const fastListFolder = function(options, callback) { | ||
const pathJoin = function(dir, name, isDir) { | ||
dir = dir.replace(/\\/g, '/'); | ||
const sep = dir.endsWith('/') ? '' : '/'; | ||
let p = dir + sep + name; | ||
p = p.replace(/\\/g, '/'); | ||
isDir && name && (p += '/'); | ||
return p; | ||
}; | ||
const readdir = function stat(dir, cb) { | ||
if (!dir || !cb) throw new Error('stat(dir, cb[, concurrency])'); | ||
fs.readdir(dir, function(err, files) { | ||
if (err) return cb(err); | ||
const batch = new Batch(); | ||
batch.concurrency(16); | ||
files.forEach(function(file) { | ||
const filePath = pathJoin(dir, file); | ||
batch.push(function(done) { | ||
fs.stat(filePath, done); | ||
}); | ||
}); | ||
batch.end(function(err, stats) { | ||
if (err) { | ||
console.log('readdir error:', err); | ||
cb(err); | ||
return; | ||
} | ||
stats.forEach(function(stat, i) { | ||
stat.isDir = stat.isDirectory(); | ||
stat.path = pathJoin(dir, files[i], stat.isDir); | ||
stat.isDir && (stat.size = 0); | ||
}); | ||
cb(err, stats); | ||
}); | ||
}); | ||
}; | ||
const statFormat = function(stat) { | ||
return { | ||
path: stat.path, | ||
size: stat.size, | ||
isDir: stat.isDir | ||
}; | ||
}; | ||
if (typeof options !== 'object') options = { path: options }; | ||
const rootPath = options.path; | ||
let list = []; | ||
const _callback = function(err) { | ||
if (err) { | ||
callback(err); | ||
} else if (list.length > 1000000) { | ||
callback(window.lang.t('error.too_much_files')); | ||
} else { | ||
callback(null, list); | ||
} | ||
}; | ||
const deep = function(dirStat, deepNext) { | ||
list.push(statFormat(dirStat)); | ||
readdir(dirStat.path, function(err, files) { | ||
if (err) return deepNext(); | ||
const dirList = files.filter(file => file.isDir); | ||
const fileList = files.filter(file => !file.isDir); | ||
list = [].concat(list, fileList.map(statFormat)); | ||
Async.eachLimit(dirList, 1, deep, deepNext); | ||
}); | ||
}; | ||
fs.stat(rootPath, function(err, stat) { | ||
if (err) return _callback(); | ||
stat.isDir = true; | ||
stat.path = pathJoin(rootPath, '', true); | ||
stat.isDir && (stat.size = 0); | ||
deep(stat, _callback); | ||
}); | ||
}; | ||
exports.simpleListFolder = simpleListFolder; | ||
exports.fastListFolder = fastListFolder; | ||
exports.createFile = createFile; |
@@ -0,0 +0,0 @@ /** |
{ | ||
"name": "cos-nodejs-sdk-v5", | ||
"version": "2.9.15", | ||
"version": "2.9.17", | ||
"description": "cos nodejs sdk v5", | ||
"main": "index.js", | ||
"types": "types", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
@@ -40,2 +40,3 @@ "demo": "node demo/demo.js", | ||
"devDependencies": { | ||
"batch": "^0.6.1", | ||
"mocha": "^4.0.1", | ||
@@ -42,0 +43,0 @@ "nyc": "^15.1.0", |
@@ -0,0 +0,0 @@ # cos-nodejs-sdk-v5 |
@@ -816,2 +816,3 @@ var session = require('./session'); | ||
var isDir = stat.isDirectory(); | ||
var FileSize = fileParams.ContentLength = stat.size || 0; | ||
@@ -857,5 +858,5 @@ var fileInfo = {Index: index, TaskId: ''}; | ||
// 添加上传任务 | ||
var api = FileSize > SliceSize ? 'sliceUploadFile' : 'putObject'; | ||
var api = FileSize <= SliceSize || isDir ? 'putObject' : 'sliceUploadFile'; | ||
if (api === 'putObject') { | ||
fileParams.Body = fs.createReadStream(fileParams.FilePath); | ||
fileParams.Body = isDir ? '' : fs.createReadStream(fileParams.FilePath); | ||
fileParams.Body.isSdkCreated = true; | ||
@@ -1045,3 +1046,3 @@ } | ||
if (FileSize === undefined || !FileSize) { | ||
callback(util.error(new Error('get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.'))); | ||
callback(util.error(new Error('get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.( 获取Content-Length失败,请在CORS ExposeHeader设置中添加Content-Length,请参考文档:https://cloud.tencent.com/document/product/436/13318 )'))); | ||
return; | ||
@@ -1153,3 +1154,3 @@ } | ||
if (FileSize === undefined || !FileSize) { | ||
callback(util.error(new Error('get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.'))); | ||
callback(util.error(new Error('get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.( 获取Content-Length失败,请在CORS ExposeHeader设置中添加Content-Length,请参考文档:https://cloud.tencent.com/document/product/436/13318 )'))); | ||
return; | ||
@@ -1156,0 +1157,0 @@ } |
@@ -0,0 +0,0 @@ var eachLimit = function (arr, limit, iterator, callback) { |
@@ -62,2 +62,6 @@ 'use strict'; | ||
} | ||
if (util.isWeb()) { | ||
console.warn('warning: cos-nodejs-sdk-v5 不支持浏览器使用,请改用 cos-js-sdk-v5,参考文档: https://cloud.tencent.com/document/product/436/11459'); | ||
console.warn('warning: cos-nodejs-sdk-v5 does not support browsers. Please use cos-js-sdk-v5 instead, See: https://cloud.tencent.com/document/product/436/11459'); | ||
} | ||
event.init(this); | ||
@@ -64,0 +68,0 @@ task.init(this); |
@@ -0,0 +0,0 @@ var initEvent = function (cos) { |
@@ -0,0 +0,0 @@ var { Transform } = require('stream'); |
@@ -0,0 +0,0 @@ var util = require('./util'); |
@@ -0,0 +0,0 @@ var session = require('./session'); |
@@ -628,2 +628,6 @@ 'use strict'; | ||
var isWeb = function () { | ||
return typeof window === 'object'; | ||
} | ||
var util = { | ||
@@ -661,4 +665,5 @@ noop: noop, | ||
obj2str: obj2str, | ||
isWeb: isWeb, | ||
}; | ||
module.exports = util; |
@@ -0,0 +0,0 @@ var fs = require('fs'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
716555
24
14929
4
13
2