Coder Social home page Coder Social logo

lua-resty-oss's Introduction

lua-resty-oss

阿里云oss lua sdk,基于openresty

使用方法

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

TODO

完整实现所有api,参考阿里云OSS API文档

基本功能是没有问题的,欢迎使用

如果你觉得不错,并在项目中使用到它,还可以向我捐献,鼓励鼓励我,金额不限

lua-resty-oss's People

Contributors

362228416 avatar

Watchers

James Cloos avatar Charles avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.