使用Golang进行S3的分块上传,并带有自定义字段。

huangapple go评论100阅读模式
英文:

Multipart upload to S3 with custom fields in golang

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我是golang的新手,我有以下类似于Python的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "os"
  8. )
  9. func SendPostRequest(url string, filename string) (string, []byte) {
  10. client := &http.Client{}
  11. data, err := os.Open(filename)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. req, err := http.NewRequest("POST", url, data)
  16. req.Header.Set("Content-Type", "multipart/form-data")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. resp, err := client.Do(req)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. content, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. return resp.Status, content
  29. }
  30. func main() {
  31. status, content := SendPostRequest("https://bucket.s3.amazonaws.com/", "what.html")
  32. fmt.Println(status)
  33. fmt.Println(string(content))
  34. }

所以我从S3得到了以下错误:

"您的POST请求的主体不是格式正确的multipart/form-data。"

附注:我已经在S3存储桶策略中给予了完全访问权限,并且我正在尝试在没有任何凭据的情况下上传。稍后我将更新此代码,以便仅在我的VPC中工作。

英文:

I am new to golang , I have python code like below

  1. import requests
  2. url = "https://bucket.s3-us-east-1.amazonaws.com/"
  3. data = { 'key': 'test.txt' }
  4. files = { 'file': open('/tmp/test.txt', 'rb') }
  5. r = requests.post(url, data=data, files=files)
  6. print "status %s" % r.status_code

This works fine in python to upload a file , how do i do the same in GOlang , I tried something like below

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "os"
  8. )
  9. func SendPostRequest(url string, filename string) (string, []byte) {
  10. client := &http.Client{}
  11. data, err := os.Open(filename)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. req, err := http.NewRequest("POST", url, data)
  16. req.Header.Set("Content-Type", "multipart/form-data")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. resp, err := client.Do(req)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. content, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. return resp.Status, content
  29. }
  30. func main() {
  31. status, content := SendPostRequest("https://bucket.s3.amazonaws.com/", "what.html")
  32. fmt.Println(status)
  33. fmt.Println(string(content))
  34. }

So I got following error from S3

  1. The body of your POST request is not well-formed multipart/form-data.

P.s : I have given full access to my S3 bucket policy and I am trying to upload without any credentials.Later I will update this to work only with my VPC .

答案1

得分: 1

我已经使用 aws-sdk-for-go 与 S3 进行交互。你可以尝试使用它。大多数 API 都有示例,所以很容易快速开始你的第一个版本。在这里查看 - https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.CreateMultipartUpload

英文:

I have used aws-sdk-for-go for interacting with S3. You can try that. Most APIs have examples so it is fairly quick to get your first version going. Check here - https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.CreateMultipartUpload.

答案2

得分: 1

为您提供一种选择,您可以使用一个类似于Python的requests库的Go语言库。这将帮助您将Python代码转换为Golang。这是一个很棒的库:https://github.com/levigross/grequests。

您还可以在Awesome-Go上找到其他库的选择:https://github.com/avelino/awesome-go。

英文:

One option for you is to use a Go library which behaves like Python's requests library. This would help you in translating your Python code to Golang. Here is a wonderful library: https://github.com/levigross/grequests .

You can find other choices for libraries at Awesome-Go:
https://github.com/avelino/awesome-go .

huangapple
  • 本文由 发表于 2022年8月21日 05:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/73430312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定