0
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 3 years have passed since last update.

Add custom header to S3 CreateBucket request

Last updated at Posted at 2021-11-09

Sample code for adding custom header to CreateBucket request.
AWS-SDK's CreateBucket does not let you add custom headers so you have to create your own http request.


import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/aws/aws-sdk-go/aws/credentials"
	v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
)

func createBucket() {

	endpoint := "https://xxx.xxx.xxx/"
	region := "region-1"
	bucketName := "bucket-name"

	// Specify accesskey secretkey or read from env with NewEnvCredentials()

	cred := credentials.NewEnvCredentials()

	// cred := credentials.NewStaticCredentialsFromCreds(credentials.Value{
	// 	AccessKeyID:     akey,
	// 	SecretAccessKey: skey,
	// })

	signer := v4.NewSigner(cred)

	httpClient := &http.Client{}

	// Create a PUT request

	req, err := http.NewRequest(http.MethodPut, endpoint+bucketName, nil)
	if err != nil {
		fmt.Print(err)
	}

	// Now add whatever header you want

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("HEADER-TO-BE-ADDED", "HEADER-TO-BE-ADDED")

	// Sign it

	signer.Sign(req, nil, "s3", region, time.Now())

	// Send the request
	res, err := httpClient.Do(req)
	log.Println(res.Status)

}```
0
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
0
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?