Alibaba CloudOSS画像圧縮アクセスopenrestyluaヘッダー署名(Alibaba Cloud OSS Nginx署名プロキシモジュール)

oss_auth.luasigned_subresourcesは「x - oss -process」を追加して  画像処理を実現します

追加

1.この記事は主にコンテンツを紹介します

この記事では、主にNginxluaを使用してAlibabaCloudOSSストレージスペースをローカルディスクとして使用する方法を紹介します。コアは、Nginx luaを使用してOSSリクエストに署名し、内部ジャンプを使用してすべてのリクエストを転送してローカルNginxとOSS署名にアクセスすることです。これにより、ローカルNginxは無制限のストレージスペース拡張と無制限のストレージコストでAlibaba CloudOSSにシームレスに接続できます。 。データセキュリティ%99.99……。

2.この記事で使用されているいくつかのツールとテクニック、および学習と習得の方法

1、lua

この記事はいくつかの基本的なluaを使用し、基本的に30分かけてluaの文法を読みます。この記事の内容は、簡単に理解できます。

2、Nginx lua

主にnginxluaと環境のデプロイを学習しますが、この記事を読んでnginx lua環境を自分で学習してデプロイする必要はありません。読者は、公式のDockerミラーソースからopenrestyミラーを取得して実験できます。この記事は実験環境としてopenresty / 1.7.7.2です。

3. Alibaba Cloud OSS

急いでアクティベートしてください。使用するのはかなりクールです。1つのストアから世界に無制限にアクセスでき
ますhttps://www.aliyun.com/act/aliyun/ossdoc.html

4.リファレンスブログ

インターネットhttp://blog.csdn.net/sunrain_chy/article/details/50804410で提供されているOSSおよびHTTPサービスをより深く理解するために、私の別のブログを読むことをお勧めします。

3. Nginx luaを使用してリクエスト署名を実装し、OSSに転送します

Lua署名コード
注:このコードは作成者によるものではありません
oss_auth.lua


-- has been sorted in alphabetical order
local signed_subresources = {
   'acl',
   'append',
   'bucketInfo',
   'cname',
   'commitTransition',
   'comp',
   'cors',
   'delete',
   'lifecycle',
   'location',
   'logging',
   'mime',
   'notification',
   'objectInfo',
   'objectMeta',
   'partData',
   'partInfo',
   'partNumber',
   'policy',
   'position',
   'referer',
   'replication',
   'replicationLocation',
   'replicationProgress',
   'requestPayment',
   'response-cache-control',
   'response-content-disposition',
   'response-content-encoding',
   'response-content-language',
   'response-content-type',
   'response-expires',
   'restore',
   'security-token',
   'tagging',
   'torrent',
   'uploadId',
   'uploads',
   'versionId',
   'versioning',
   'versions',
   'website',
   'x-oss-process'
}

function string.startswith(s, start)
   return string.sub(s, 1, string.len(start)) == start
end

local function get_canon_sub_resource()
   local args = ngx.req.get_uri_args()
   -- lower keys
   local keys = {}
   for k, v in pairs(args) do
      keys[k:lower()] = v
   end
   -- make resource string
   local s = ''
   local sep = '?'
   for i, k in ipairs(signed_subresources) do
      v = keys[k]
      if v then
         -- sub table
         v = type(v) == 'table' and v[1] or v
         s = s .. string.format("%s%s=%s", sep, k, v)
         sep = '&'
      end
   end
   return s
end

local function get_canon_resource()
   resource = ''
   object = ngx.unescape_uri(ngx.var.uri)
   sub = get_canon_sub_resource()   
   return string.format("/%s%s%s", ngx.var.oss_bucket , object, sub)
end   

local function get_canon_headers()
   -- default: <lowerkey, value>
   local headers = ngx.req.get_headers()
   local keys = {}
   for k, v in pairs(headers) do
      if string.startswith(k, 'x-oss-') then
         -- client must assemble the same header keys
         if type(v) ~= 'string' then return nil end
         table.insert(keys, k)
      end
   end
   -- sorted in alphabetical order
   table.sort(keys)
   for i, key in ipairs(keys) do
      keys[i] = key .. ':' .. headers[key] .. '\n'
   end
   return table.concat(keys)
end

local function calc_sign(key, method, md5, type_, date, oss_headers, resource)
    -- string_to_sign:
    -- method + '\n' + content_md5 + '\n' + content_type + '\n'
    -- + date + '\n' + canonicalized_oss_headers + canonicalized_resource
    local sign_str = string.format('%s\n%s\n%s\n%s\n%s%s',
    method, md5, type_,
    date, oss_headers, resource)
    ngx.log(ngx.ERR, "SignStr:", sign_str, "\n")
    local sign_result = ngx.encode_base64(ngx.hmac_sha1(key, sign_str))
    return sign_result, sign_str
end   

local function oss_auth()
   -- ngx.log(ngx.INFO, 'auth')
   --local method = ngx.var.request_method
   local method = ngx.req.get_method()
   
   local content_md5 = ngx.var.http_content_md5 or ''
   
   local content_type = ngx.var.http_content_type or ''
  
   -- get date
   local date = ngx.var.http_x_oss_date or ngx.var.http_date or ''
   if date == '' then
      date = ngx.http_time(ngx.time())
      -- ngx.log(ngx.INFO, 'Date:', date)
      ngx.req.set_header('Date', date)
   end
   ngx.req.set_header('Date', ngx.http_time(ngx.time()))
   local resource = get_canon_resource()
   local canon_headers = get_canon_headers()
   local sign_result, sign_str = calc_sign(ngx.var.oss_auth_key, method, content_md5,
   content_type, date, canon_headers, resource)
   -- ngx.log(ngx.INFO, 'sign string:', sign_str)
   -- ngx.log(ngx.INFO, 'sign string len:', string.len(sign_str))
   local auth = string.format("OSS %s:%s", ngx.var.oss_auth_id, sign_result)
   ngx.req.set_header('Authorization', auth)

   ngx.log(ngx.EMERG,"<-- end :" .. auth)
    local headers = ngx.req.get_headers()
for k, v in pairs(headers) do

     ngx.log(ngx.EMERG,"[key]:",k," [v:",v)
end
   ngx.exec("@oss")
end   

-- main
res = oss_auth()

if res then
   ngx.exit(res)
end

nginx.conf

  server {
        listen 8000;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_request_buffering off;

        location / {
            set $oss_bucket "your_oss_bucket";
            set $oss_auth_id "your_access_id";
            set $oss_auth_key "your_access_key";
            rewrite_by_lua_file "/path/oss_auth.lua";
        }

        # internal redirect
        location @oss {
            // endpoint eg: oss.aliyuncs.com
            proxy_pass http://your_oss_bucket.endpoint; 
        }
    }

4.上記のコードの使用方法

まず、oss_auth.luaを変更する必要はありません。

nginx.confで、
your_oss_bucketを
Alibaba Cloud OSSのバケット名置き換える必要があります
。your_access_idはAccessKeyIdなしで、your_access_keyはAccessKeySecret付きです。

例えば:

error_log  logs/error.log  debug;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    lua_package_path "/usr/servers/lualib/?.lua;";
    lua_package_cpath "/usr/servers/lualib/?.so;"; 
    server {
        listen       80;
        location / {
            set $oss_bucket "bucket-example";
            set $oss_auth_id "za2127hbbsdhjal0ytocbzr";
            set $oss_auth_key "gMOG3o+HJdsgdHdpieCNMcsaH+Q=";
            rewrite_by_lua_file conf/lua/oss_auth.lua;
        }

        location @oss {
            proxy_pass http://bucket-example.oss-cn-qingdao.aliyuncs.com;
        }
    }
}

アクセス  

http://127.0.0.1/20200717/1594973763512-7a892062-1865-4006.jpg?x-oss-process=image/auto-orient,1/resize,m_lfit,w_400/quality,q_67

おすすめ

転載: blog.csdn.net/csl12919/article/details/107772148