阿里云OSS对象存储的Go服务端签名直传扩展

alphasnow · · 118 次点击 · 开始浏览    置顶

# aliyun-oss-appserver-go 通过Web端上传数据到OSS. 服务端签名直传并设置上传回调. ## 安装 [https://github.com/alphasnow/aliyun-oss-appserver-go](https://github.com/alphasnow/aliyun-oss-appserver-go) ```shell go get -u github.com/alphasnow/aliyun-oss-appserver-go ``` ## 示例 ### 生成授权 ```go token := appserver.NewToken(&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/" //} ``` ## 上传文件 ```bash 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"' ``` ## 回调验证 ```go 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" //} ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

118 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传