openresty 整合阿里云 oss

目前阿里云官方并未提供lua版的sdk,在网上找了几个,感觉不是很理想,于是自己造了一个轮子,目前还是一个单车的轮子,只实现了部分功能,不过也能用了

废话不多说上代码


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方法进行上传

文件上传模块可以用lua-resty-upload来处理

lua代码参考:


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)

前端代码参考

<!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        上传文件

  • delete_object     删除文件

  • put_bucket        创建bucket

  • put_bucket_acl    修改bucket权限

  • delete_bucket     删除bucket

代码已上传github,连接:https://github.com/362228416/lua-resty-oss

更多openresty lua相关内容可以点击这里获取

    原文作者:外星人
    原文地址: https://segmentfault.com/a/1190000009117957
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞