文件存储微服务-- 基于 beego 开发

文件存储微服务


上传文件是通用需求,所以我们最好把这个功能抽离出来,做成通用的,避免每次都重复写。

项目地址

安装

    go get -u -v github.com/gamelife1314/filestore

修改配置

   修改配置文件 conf/app.conf

启动服务

  go run main.go

启动服务之后,会自动创建相关的数据表,无须手动创建的。

示例

  1. 创建一个 Client ,可以把这个 Client 想象成一个站点。

    $  http POST http://127.0.0.1:8080/v1/client
        HTTP/1.1 200 OK
        Access-Control-Allow-Credentials: true
        Access-Control-Allow-Headers: Origin, Content-Type, Accept
        Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
        Access-Control-Allow-Origin: 127.0.0.1
        Content-Length: 157
        Content-Type: application/json; charset=utf-8
        Date: Sun, 29 Jul 2018 03:27:40 GMT
        Server: beegoServer:1.10.0
    
        {
            "code": 2000,
            "data": {
                "client_id": "6949186648068646",
                "client_secret": "&gT#MY,q~NFO"
            },
            "message": "create client successfully!"
        }
  2. 申请 token 用于随后的文件上传以及下载

    $ http --form  http://127.0.0.1:8080/v1/token client_id="6949186648068646" client_secret="&gT#MY,q~NFO" expires=5000000
        HTTP/1.1 200 OK
        Access-Control-Allow-Credentials: true
        Access-Control-Allow-Headers: Origin, Content-Type, Accept
        Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
        Access-Control-Allow-Origin: 127.0.0.1
        Content-Length: 231
        Content-Type: application/json; charset=utf-8
        Date: Sun, 29 Jul 2018 03:29:33 GMT
        Server: beegoServer:1.10.0
    
        {
            "code": 2012,
            "data": {
                "client_id": "6949186648068646",
                "created_at": "2018-07-29 11:29:33",
                "expires": 1537834973,
                "token": "e45823d7246578a07fc7675672e25342"
            },
            "message": "token create successfully!"
        }
  3. 上传文件,会上传到配置项 uploadFilesDir 目录中。

    $ http --form http://127.0.0.1:8080/v1/file/upload token=e45823d7246578a07fc7675672e25342 path=/ file@~/Desktop/2.jpg
        HTTP/1.1 200 OK
        Access-Control-Allow-Credentials: true
        Access-Control-Allow-Headers: Origin, Content-Type, Accept
        Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
        Access-Control-Allow-Origin: 127.0.0.1
        Content-Length: 118
        Content-Type: application/json; charset=utf-8
        Date: Sun, 29 Jul 2018 03:31:11 GMT
        Server: beegoServer:1.10.0
    
        {
            "code": 2030,
            "data": {
                "slug": "cab424f5733d8d63173c6d4218e23a18"
            },
            "message": "uplod file success!"
        }
  4. 每个文件都有一个标识,通过这个标识下载相应的文件。

    $ http --download  http://127.0.0.1:8080/v1/file/download token==e45823d7246578a07fc7675672e25342 slug==cab424f5733d8d63173c6d4218e23a18
        HTTP/1.1 200 OK
        Accept-Ranges: bytes
        Access-Control-Allow-Credentials: true
        Access-Control-Allow-Headers: Origin, Content-Type, Accept
        Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
        Access-Control-Allow-Origin: 127.0.0.1
        Cache-Control: public
        Content-Description: File Transfer
        Content-Disposition: attachment; filename=2.jpg
        Content-Length: 3642
        Content-Transfer-Encoding: binary
        Content-Type: image/jpeg
        Date: Sun, 29 Jul 2018 03:33:20 GMT
        Expires: 0
        Last-Modified: Sun, 29 Jul 2018 03:31:11 GMT
        Pragma: public
        Server: beegoServer:1.10.0
    
        Downloading 3.56 kB to "2.jpg"
        Done. 3.56 kB in 0.00108s (3.22 MB/s)
本作品采用《CC 协议》,转载必须注明作者和本文链接

好好写代码,娶个宝宝回家。

Michael001
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!