(转)七牛云文件上传aardio调用库分享
By
admin
at 2021-02-28 • 0人收藏 • 1628人看过
感谢: 周 (332XXX281) 分享
qiniuyun.aardio 库代码如下:
import web.rest.jsonClient; import crypt.hmac; import crypt.bin; import fsys.ini namespace qiniuyun; class auth{ ctor(accessKey,secretKey){ this = ..web.rest.jsonClient() this.accessKey = accessKey this.secretKey = secretKey; this.ini = ..fsys.ini("/config.ini") base64_urlSafeEncode = function(str){ str = ..string.replace(str,'+', '-'); return ..string.replace(str,'/', '_'); } uploadToken = function(bucket,fileName,override){ var scope = bucket; if (override == true ) { scope += ':' ++ fileName; } var deadline = tonumber(..time()) + 3600; var putPolicy = ..string.format('{"scope":"%s","deadline":%i}' ,scope,deadline) var encoded = ..crypt.bin.encodeBase64(putPolicy) var encoded_signed = base64_urlSafeEncode(..crypt.bin.encodeBase64( ..crypt.hmac.sha1(this.secretKey , encoded ).getValue() )) return this.accessKey + ":" + encoded_signed + ":" + encoded; } }; uploadFile = function(bucket,filePath,fileName,override){ var url = this.ini.read("http","up") if(url == null){ var res = this.api('https://uc.qbox.me/v1/query?ak='++ this.accessKey ++'&bucket='++bucket).get() url = res.http.up[1] this.ini.write("http","up", url ) } if(fileName == null){ fileName = ..io.splitpath(filePath).file } var token = uploadToken(bucket,fileName,override) ; var restApi = this.api(url++"/") this.sendMultipartForm( { key = fileName; token = token; file = "@"++filePath; } ) return restApi(); } }
调用示例: 注意下面的key请去七牛云申请,然后替换为你申请到的
accessKey = 'accessKey '; secretKey = 'secretKey '; bucket = 'bucket '; qiniu = qiniuyun.auth(accessKey,secretKey) console.dump(qiniu.uploadFile(bucket,'E:/q.txt','haha.txt')) //普通上传 console.dump(qiniu.uploadFile(bucket,'E:/q.txt','haha.txt',true)) //覆盖上传
4 个回复 | 最后更新于 2021-04-11
jacen_he
2021-03-04
#1
登录后方可回帖