openresty 整合阿里雲 oss

目前阿里雲官方並未提供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相關內容能夠點擊這裏獲取

相關文章
相關標籤/搜索