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

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

Multipart upload to S3 with custom fields in golang

问题

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

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

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

func SendPostRequest(url string, filename string) (string, []byte) {
    client := &http.Client{}
    data, err := os.Open(filename)
    if err != nil {
        log.Fatal(err)
    }
    req, err := http.NewRequest("POST", url, data)
	req.Header.Set("Content-Type", "multipart/form-data")

    if err != nil {
        log.Fatal(err)
    }
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    return resp.Status, content
}

func main() {
    status, content := SendPostRequest("https://bucket.s3.amazonaws.com/", "what.html")
    fmt.Println(status)
    fmt.Println(string(content))
}

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

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

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

英文:

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

import requests
url = "https://bucket.s3-us-east-1.amazonaws.com/"
data = { 'key': 'test.txt' }
files = { 'file': open('/tmp/test.txt', 'rb') }
r = requests.post(url, data=data, files=files)
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

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

func SendPostRequest(url string, filename string) (string, []byte) {
    client := &http.Client{}
    data, err := os.Open(filename)
    if err != nil {
        log.Fatal(err)
    }
    req, err := http.NewRequest("POST", url, data)
	req.Header.Set("Content-Type", "multipart/form-data")

    if err != nil {
        log.Fatal(err)
    }
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    return resp.Status, content
}

func main() {
    status, content := SendPostRequest("https://bucket.s3.amazonaws.com/", "what.html")
    fmt.Println(status)
    fmt.Println(string(content))
}

So I got following error from S3

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:

确定