2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Golang、PresignURLでファイルをアップロードするときハマったところ

Posted at

GolangでPresignURLを使ってファイルをアップロードすると下記のエラーが出てしまい

<Error><Code>NotImplemented</Code>
<Message>A header you provided implies functionality that is not implemented</Message>
<Header>Transfer-Encoding</Header>

RequestのHeaderをDumpするとTransfer-Encoding:chunkedになってる。この値だとAWS側が対応してないみたいです。

解決方法

go version 1.19

req.ContentLength = #content size <- ファイルのサイズ値を設定する

ContentLengthを設定するだけで解決しました

因みに、req.Header.Set("Content-Length", xxx)を設定しても解決できないです。これのせいで結構ハマりました理由です。

Full Codeはここです

func uploadFile(filePath, contentType, uploadUrl string) {
	data, err := os.Open(filePath)
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	req, err := http.NewRequest("PUT", uploadUrl, data)
	if err != nil {
		log.Fatal(err)
	}

	info, _ := data.Stat()
	req.Header.Set("Content-Type", contentType)
	req.ContentLength = info.Size()

	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	if res.StatusCode == http.StatusOK {
		log.Println("Upload S3 Success ", filePath)
	} else {
		log.Println("Upload S3 Fail ", filePath)
	}

	respDump, err := httputil.DumpResponse(res, true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Response:\n%s", string(respDump))
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?