如何打造基层警营文化品牌如何打造基于s3cmd的短地址服务

2022-12-07 0 29

如何打造基于s3cmd的短地址服务

这篇文章次要讲解了“如何打造基于s3cmd的短地址服务”文中的讲解内容简单清晰易于学习与理解下面请大家跟着小编的思路慢慢深入一起来研究和学习“如何打造基于s3cmd的短地址服务”吧!

需求描述

有线上项目使用S3去发布移动端APP由于需要做权限认证所以对应的Object无法开“public-read”权限尝试使用Presign方式生成的签名url存在以下问题

生成的签名URL地址包含敏感信息会暴露bucket名称和accesskey带来安全隐患。

生成的签名URL地址太长影响用户体验。

生成的地址无效时间内可以任意访问无法做到比较严格的反盗链。

处理软件下载

使用openresty实现对S3数据访问流程的封装客户端的数据下载全部走openresty上面搭建的服务该服务提供一次性的短地址生成服务访问几次当前对应短地址失效具体流程如下:

1. 用户数据上传到S3并设置相应Object权限为“public-read”取得对应的对外访问URL。

2. 以之前生成的对外访问URL为基础生成对应的短地址服务。

3. 客户端使用短地址进行访问超出访问次数则无法访问。

如果想进一步提升安全性可以将RGW和Openresty通过内网互联数据上传全部走内部网络外部访问走外网。

具体实现

1. Openresty安装2.安装依赖lua库文件

默认openre软件下载sty的lib路径为/usr/local/openresty/lualib

需要依赖两个lua文件其中url.lua次要用于url地址的解析redis_iresty.lua用于redis的连接将下面的源码文件保存为 /usr/local/openresty/lualib/resty/url.lua
https://github./golgote/neturl/blob/master/lib/net/url.lua

将下面的源码文件保存为 /usr/local/openresty/lualib/resty/redis_iresty.lua
https://gist.gith软件下载ub./moonbingbing/9915c66346e8fddcefb53. RGW服务配置rootdemohost:/etc/openresty# cat /etc/ceph/ceph.conf

[client.radosgw.demo]
     rgw dns name = s3.cephbook. #S3 endpoint对应的域名
     rgw frontends = “civetweb port=7480”host = demohost
     keyring = /etc/ceph/ceph.软件下载client.radosgw.keyringlog file = /home/ceph/log/radosgw.log4. Openresty配置rootdemohost:/etc/openresty# cat /etc/openresty/nginx.conf

server {
        listen       80;
        server_name  ceph.vip; #短地址主机头设置
        location = /url { #生成短地址入口
             content_by_lua_file /etc
/openresty/url.l软件下载ua;
         }
         location / {#提供短地址对应的数据访问
             proxy_http_version 1.1;
             proxy_set_header Host $host;
             access_by_lua_file /etc/openresty/access.lua;
             proxy_pass http://127.0.0.1:7480#对应后端的radosgw服务入口
         }
     }

生成短地址服务的软件下载源码如下

rootdemohost:/etc/openresty# cat url.lua
local request_method = ngx.var.request_method
local url_ = require “resty.url” #对应之前的url库
local redis = require “resty.redis_iresty” #对应之前的redis_iresty库
local s3_endpoint = “s3.cephbook.” #这里设置只允许生成与S3 endpoint相关的短地址
local endpoint = s3_endpoint .. “$”

local charset = {}
f软件下载or i = 48,  57 do table.insertcharset, string.charend
for i = 65,  90 do table.insertcharset, string.charend
for i = 97122 do table.insertcharset, string.charend

function string.randomlength
  local urandom = assertio.open/dev/urandom,rb
  local a, b, c, d = urandom:read4:byte1,4
  urandom:close软件下载local seed = a*0x1000000 + b*0x10000 + c *0x100 + d
  math.randomseedseed

  if length > 0 then
    return string.randomlength – 1 .. charset[math.random1, #charset]
  else
    return “”
  end
end

function genera_urlred,url_id,host,path
    — counts表示短地址访问次数current表示当前次数
    ok, err = red:hmseturl_id,host,host,uri,path,counts,3,cur软件下载rent,1
    if not ok then
        ngx.status = ngx.HTTP_METHOD_NOT_IMPLEMENTED
        ngx.say“failed to hmset: “, err
        return ngx.exitngx.HTTP_METHOD_NOT_IMPLEMENTED
    end
    — 设置短地址记录最多能够在redis里面存储的时长避免资源耗尽
    ok, err = red:expireurl_id,3600 
    if not ok then
        ngx.status= ngx.HTTP_METHOD_NOT_IMPLEMENTED
        n软件下载gx.say“failed to set expire: “, err
        return ngx.exitngx.HTTP_METHOD_NOT_IMPLEMENTED
    end
end

function get_info_by_idred,url_id
    ok, err = red:hgetallurl_id
    if not ok then
        ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
        ngx.say“failed to getall : “, err
        return ngx.exitngx.HTTP_SERVICE_UNAVAILABLE软件下载end
    local h = red:array_to_hashok
    return h
end

if request_method == “POST” then
    ngx.req.read_body
    local data = ngx.req.get_body_data
    local u = url_.parsedata
    local hostname_check, hostname_err = ngx.re.matchu.host,endpoint
    if not hostname_check then
        ngx.status = ngx.HTTP_BAD_REQUEST
        ngx.say“not软件下载 a available s3_endpoint”
        ngx.exitngx.HTTP_BAD_REQUEST
    end
    if u.host then
        if string.lenu.path > 1  then
            local url_id = string.random7
            local red = redis:new
            genera_urlred,url_id,u.host,u.path
            local h = get_info_by_idred,url_id
            ngx.status= ngx.HTTP_软件下载CREATED
            ngx.say“ShortURL: “,ngx.var.scheme,“://”,ngx.var.host,“/”,url_id
            ngx.say“host: “, h.host
            ngx.say“uri: “, h.uri
            ngx.say“counts: “, h.counts
            ngx.say“current: “, h.current
     软件下载       ngx.exitngx.HTTP_CREATED
        else
            ngx.status = ngx.HTTP_BAD_REQUEST
            ngx.say“path err”
            ngx.exitngx.HTTP_BAD_REQUEST
        end
    else
        ngx.status = ngx.HTTP_BAD_REQUEST
        ngx.say“not a available url”
        ngx.exitngx.HTTP_BAD_REQUEST
    end
end

访软件下载问短地址数据服务的源码如下

rootdemohost:/etc/openresty# cat /etc/openresty/access.lua
local redis = require “resty.redis_iresty”
local red = redis:new
local uri =  ngx.var.uri
local url_id = string.suburi,2,string.lenuri
res, err = red:hgetallurl_id
if not res then
    ngx.exitngx.HTTP_NOT_FOUND
else
    localh = red:ar软件下载ray_to_hashres— 超出访问次数则拒绝访问如果考虑资源占用可以本人加上删除对应redis记录操作
    if h.counts < h.current then
        ngx.status = ngx.HTTP_GONE
        ngx.say“current=”,h.current,” > counts=”,h.counts
        ngx.exitngx.HTTP_GONE
    end
    ngx.req.set_header“host”, h.host
    ngx.req.set_urih.uri, false
软件下载     ok, err = red:hincrbyurl_id,current,1
    if not ok then
        ngx.status = ngx.HTTP_METHOD_NOT_IMPLEMENTED
        ngx.say“incrby key failed: “, err
        ngx.exitngx.HTTP_METHOD_NOT_IMPLEMENTED
        return
    end
end

github./openresty/lua-nginx-module#http-status-constants

5. 使用流程上传数软件下载

使用s3cmd或者是其他方式上传文件并设置对应的Object访问权限为”public-read”。

如何打造基层警营文化品牌如何打造基于s3cmd的短地址服务

rootdemohost:/tmp# s3cmd put myfile s3://demo –acl-public
myfile -> s3://demo/myfile  [1 of 1]
 21577 of 21577   100% in    0s   227.71 kB/s  done
myfile -> s3://demo/myfile  [1 of 1]
 21577 of 21577   100% in    0s   203.11 kB/s  done
Public URL of the object is: demo.s3.cephbook./myfile生成短地址root软件下载demohost:/tmp# curl ceph.vip/url  -d “demo.s3.cephbook./myfile” -v
* Hostname was NOT found in DNS cache
* Connected to ceph.vip  port 80 #0
> POST /url HTTP/1.1
> User-Agent: curl/7.38.0
> Host: ceph.vip
> Accept: */*
> Content-Length: 29
> Content-Type: application/x–form-urlencoded
>
* upload pletely sent off: 29 out of 29 bytes软件下载
< HTTP/1.1 201 Created
* Server openresty/1.11.2.5 is not blacklisted
< Server: openresty/1.11.2.5
< Date: Wed, 15 Nov 2017 09:56:14 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
ShortURL: ceph.vip/Vo3t5Ex #生成的短地址
host: demo.s3.cephbook.
uri: /myfile
counts: 3
current: 1
* Conn软件下载ection #0 to host ceph.vip left intact测试访问#访问3
rootdemohost:/tmp# curl http://ceph.vip/Vo3t5Ex -v

rootdemohost:/tmp# curl http://ceph.vip/Vo3t5Ex -v

rootdemohost:/tmp# curl http://ceph.vip/Vo3t5Ex -v

#第四次当前就不能访问了

root软件下载demohost:/tmp# curl http://ceph.vip/Vo3t5Ex -v
* Hostname was NOT found in DNS cache
*   Trying ceph.vip…
* Connected to ceph.vip ceph.vip port 80 #0
> GET /Vo3t5Ex HTTP/1.1
> User-Agent: curl/7.38.0
> Host: ceph.vip
> Accept: */*
>
< H软件下载TTP/1.1 410 Gone
* Server openresty/1.11.2.5 is not blacklisted
< Server: openresty/1.11.2.5
< Date: Wed, 15 Nov 2017 10:00:46 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
软件下载 <
current=4 > counts=3
* Connection #0 to host ceph.vip left intact

感激各位的阅读以上就是“如何打造基于s3cmd的短地址服务”的内容了经过本文的学习后相信大家对如何打造基于s3cmd的短地址服务这一问题有了更深刻的体会具体使用情况还需要大家实践验证。这里是小编将为大家推送更多相关知识点的文章欢迎关注!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

所有文章为演示数据,平台本身不提供下载地址,版权归原作者所有,仅提供演示效果!如果有演示站与下载地址均为发布者提供,与本站无关!

1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长删除QQ : 2592229330

2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!

3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!

5. 如有链接无法下载、失效或广告,请联系管理员处理!

6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!

7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

8. 精力有限,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别

9. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!

10. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!

11. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!

朋纳云,网站模板,商业网站源码,js特效,php源码,网页特效,html模板,jquery特效,站长素材 云计算 如何打造基层警营文化品牌如何打造基于s3cmd的短地址服务 http://zy.shpnkj.com/963367.html

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用360急速浏览器!
查看详情

相关文章

发表评论
暂无评论
平台官方客服团队

为您解决烦忧 - 5*8小时在线 专业服务