Skip to content

Latest commit

 

History

History
72 lines (68 loc) · 3.67 KB

README-CN.md

File metadata and controls

72 lines (68 loc) · 3.67 KB

English | 简体中文

aliyun-oss-appserver-go

通过Web端上传数据到OSS. 服务端签名直传并设置上传回调.

安装

go get -u github.com/alphasnow/aliyun-oss-appserver-go

示例

生成授权

token := appserver.NewToken(&appserver.Config{
    AccessKeyId:     "yourAccessKeyId",
    AccessKeySecret: "yourAccessKeySecret",
    Host:            "https://bucket-name.oss-cn-hangzhou.aliyuncs.com",
    Directory:       "user-dir-prefix/",
    ExpireSecond:    600,
    // 回调只支持 PutObject, PostObject, CompleteMultipartUpload
    CallbackUrl:     "http://domain.com/oss/callback", 
})
postToken, _ := token.Generate()
postTokenJson, _ := json.Marshal(postToken)
//{
//    "OSSAccessKeyId": "yourAccessKeyId",
//    "policy": "eyJleHBpcmF0aW9uIjoiMjAyNS0wMS0wMVQwMDowMDowMFoiLCJjb25kaXRpb25zIjpbWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyLWRpci1wcmVmaXgvIl1dfQ==",
//    "callback": "eyJjYWxsYmFja1VybCI6Imh0dHA6Ly9kb21haW4uY29tL29zcy9jYWxsYmFjayIsImNhbGxiYWNrQm9keSI6IntcImJ1Y2tldFwiOiR7YnVja2V0fSxcIm9iamVjdFwiOiR7b2JqZWN0fSxcImV0YWdcIjoke2V0YWd9LFwic2l6ZVwiOiR7c2l6ZX0sXCJtaW1lVHlwZVwiOiR7bWltZVR5cGV9LFwiaW1hZ2VJbmZvXCI6e1wiaGVpZ2h0XCI6JHtpbWFnZUluZm8uaGVpZ2h0fSxcIndpZHRoXCI6JHtpbWFnZUluZm8ud2lkdGh9LFwiZm9ybWF0XCI6JHtpbWFnZUluZm8uZm9ybWF0fX0sXCJjcmM2NFwiOiR7Y3JjNjR9LFwiY29udGVudE1kNVwiOiR7Y29udGVudE1kNX0sXCJ2cGNJZFwiOiR7dnBjSWR9LFwiY2xpZW50SXBcIjoke2NsaWVudElwfSxcInJlcUlkXCI6JHtyZXFJZH0sXCJvcGVyYXRpb25cIjoke29wZXJhdGlvbn19IiwiY2FsbGJhY2tCb2R5VHlwZSI6ImFwcGxpY2F0aW9uL2pzb24ifQ==",
//    "signature": "uXL82wU5IGCd7vcZKX9gua5TUJs=",
//    "host": "https://bucket-name.oss-cn-hangzhou.aliyuncs.com",
//    "expire": 1735689600,
//    "directory": "user-dir-prefix/"
//}

上传文件

curl --location "https://bucket-name.oss-cn-hangzhou.aliyuncs.com" \
--form 'key="user-dir-prefix/${filename}"' \
--form 'policy="eyJleHBpcmF0aW9uIjoiMjAyNS0wMS0wMVQwMDowMDowMFoiLCJjb25kaXRpb25zIjpbWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyLWRpci1wcmVmaXgvIl1dfQ=="' \
--form 'OSSAccessKeyId="yourAccessKeyId"' \
--form 'callback="eyJjYWxsYmFja1VybCI6Imh0dHA6Ly9kb21haW4uY29tL29zcy9jYWxsYmFjayIsImNhbGxiYWNrQm9keSI6IntcImJ1Y2tldFwiOiR7YnVja2V0fSxcIm9iamVjdFwiOiR7b2JqZWN0fSxcImV0YWdcIjoke2V0YWd9LFwic2l6ZVwiOiR7c2l6ZX0sXCJtaW1lVHlwZVwiOiR7bWltZVR5cGV9LFwiaW1hZ2VJbmZvXCI6e1wiaGVpZ2h0XCI6JHtpbWFnZUluZm8uaGVpZ2h0fSxcIndpZHRoXCI6JHtpbWFnZUluZm8ud2lkdGh9LFwiZm9ybWF0XCI6JHtpbWFnZUluZm8uZm9ybWF0fX0sXCJjcmM2NFwiOiR7Y3JjNjR9LFwiY29udGVudE1kNVwiOiR7Y29udGVudE1kNX0sXCJ2cGNJZFwiOiR7dnBjSWR9LFwiY2xpZW50SXBcIjoke2NsaWVudElwfSxcInJlcUlkXCI6JHtyZXFJZH0sXCJvcGVyYXRpb25cIjoke29wZXJhdGlvbn19IiwiY2FsbGJhY2tCb2R5VHlwZSI6ImFwcGxpY2F0aW9uL2pzb24ifQ=="' \
--form 'signature="uXL82wU5IGCd7vcZKX9gua5TUJs="' \
--form 'file=@"~/Downloads/image.jpg"'

回调验证

aliyunOSSCallback := appserver.NewAliyunOSSCallback(request)
callbackBody,err := aliyunOSSCallback.VerifySignature()
//{
//    "bucket": "bucket-name",
//    "object": "user-dir-prefix/image.jpg",
//    "etag": "A3AC1B2FAADBD*****EE9F5EA57CAACB",
//    "size": 2788,
//    "mimeType": "image/jpeg",
//    "imageInfo": {
//        "height": 197,
//        "width": 257,
//        "format": "jpg"
//    },
//    "crc64": 34616313***72852000,
//    "contentMd5": "o6wbL6rb0***7p9epXyqyw==",
//    "vpcId": null,
//    "clientIp": "100.20.30.40",
//    "reqId": "674EB5AA2*****37341888F8",
//    "operation": "PutObject"
//}

参考