-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import qiniu from 'qiniu'; | ||
|
||
function init(param) { | ||
qiniu.conf.ACCESS_KEY = param.access_key; | ||
qiniu.conf.SECRET_KEY = param.secret_key; | ||
} | ||
|
||
function getToken() { | ||
return new qiniu.auth.digest.Mac(qiniu.conf.ACCESS_KEY, qiniu.conf.SECRET_KEY); | ||
} | ||
|
||
/** | ||
* http请求鉴权 | ||
* @param url | ||
* @returns {*} | ||
*/ | ||
function httpAuthorization(url) { | ||
return qiniu.util.generateAccessToken(getToken(), url, null) | ||
} | ||
|
||
/** | ||
* 通过url抓取文件 | ||
*/ | ||
function fetch(params, callback) { | ||
let config = new qiniu.conf.Config(); | ||
let bucketManager = new qiniu.rs.BucketManager(getToken(), config); | ||
|
||
bucketManager.fetch(params.path, params.bucket, params.key, function (respErr, respBody, respInfo) { | ||
if (respErr) { | ||
throw respErr; | ||
} | ||
callback(respErr, respBody); | ||
}); | ||
} | ||
|
||
/** | ||
* 上传文件 | ||
* @param params | ||
* @param callback | ||
*/ | ||
function upload(params, callback) { | ||
let options = { | ||
scope: params.bucket, | ||
}; | ||
let putPolicy = new qiniu.rs.PutPolicy(options); | ||
let uploadToken = putPolicy.uploadToken(getToken()); | ||
|
||
let config = new qiniu.conf.Config(); | ||
let formUploader = new qiniu.form_up.FormUploader(config); | ||
let putExtra = new qiniu.form_up.PutExtra(); | ||
|
||
formUploader.putFile(uploadToken, params.key, params.path, putExtra, function (respErr, respBody, respInfo) { | ||
if (respErr) { | ||
throw respErr; | ||
} | ||
callback(respErr, respBody); | ||
}); | ||
} | ||
|
||
/** | ||
* 删除文件操作 | ||
*/ | ||
function remove(params, callback) { | ||
let config = new qiniu.conf.Config(); | ||
let bucketManager = new qiniu.rs.BucketManager(getToken(), config); | ||
|
||
bucketManager.delete(params.bucket, params.key, function (err, respBody, respInfo) { | ||
console.log(respBody, respInfo); | ||
if (!err) { | ||
callback(respBody); | ||
} else { | ||
console.log(err); | ||
} | ||
}); | ||
} | ||
|
||
export {init, httpAuthorization, remove, upload, fetch} |