目前阿里云官方并未提供lua版的sdk,在网上找了几个,感受不是很理想,因而本身造了一个轮子,目前仍是一个单车的轮子,只实现了部分功能,不过也能用了javascript
废话很少说上代码html
local oss = require "resty.oss" local oss_config = { accessKey = "your accessKey"; secretKey = "your secretKey"; bucket = "your bucket", endpoint = "your oss endpoint" -- 例如:oss-cn-qingdao.aliyuncs.com } local client = oss.new(oss_config) local url = client:put_object("123", "text/html", "123.json") ngx.say(url) client:delete_object('123.json') client:put_bucket('test-bucket123') client:put_bucket_acl('test-bucket123', 'private') client:put_bucket_acl('test-bucket123', 'public-read') client:delete_bucket('test-bucket123')
上面的例子是直接上传文件并指定内容,文件类型,文件名前端
真实场景,多是客户端上传一个文件,而后在nginx获取到文件的内容,文件类型,而后自动生成一个文件名,再调用上面的put_object方法进行上传java
文件上传模块能够用lua-resty-upload来处理jquery
lua代码参考:nginx
local upload = require "resty.upload" local oss = require "resty.oss" -- 获取上传的文件 function readFile() local chunk_size = 4096 local form, err = upload:new(chunk_size) form:set_timeout(20000) local file = {} if not err then while true do local typ, res, err2 = form:read() if not typ then err = err2 print("failed to read: ", err2) break end if typ == 'header' and res[1] == 'Content-Disposition' then local filename = string.match(res[2], 'filename="(.*)"') file.name = filename end if typ == 'header' and res[1] == 'Content-Type' then file['type'] = res[2] end if typ == 'body' and file then file[typ] = (file[typ] or '') .. res end if typ == "eof" then break end end end return file, err end local file, err = readFile() local oss_config = { accessKey = "your accessKey"; secretKey = "your secretKey"; bucket = "your bucket", endpoint = "your oss endpoint" -- 例如:oss-cn-qingdao.aliyuncs.com } local client = oss.new(oss_config) local url = client:put_object(file.body, file.type, file.name) ngx.say(url)
前端代码参考git
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload</title> </head> <body> <input id="fileupload" type="file"/> <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="http://blueimp.github.io/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"></script> <script type="text/javascript" src="http://blueimp.github.io/jQuery-File-Upload/js/jquery.fileupload.js"></script> <script type="text/javascript"> var url = '/hello'; $('#fileupload').fileupload({ url: url, dataType: 'text', done: function (e, data) { console.log('succcess', data); }, progressall: function (e, data) { console.log(data); } }) </script> </body> </html>
put_object 上传文件github
delete_object 删除文件web
put_bucket 建立bucketjson
put_bucket_acl 修改bucket权限
delete_bucket 删除bucket
代码已上传github,链接:https://github.com/362228416/lua-resty-oss
更多openresty lua相关内容能够点击这里获取