Setup Vultr Object Storage with AWS SDK v2 and Golang

Paweł Grzesiecki
2 min readApr 29, 2021

--

Using Golang AWS SKD v2 with Vultr Object Storage is not quite easy and obvious. You must remember about few things. Let’s check them together.

First of all we need modules to communicate with AWS:

go get github.com/aws/aws-sdk-go-v2 
go get github.com/aws/aws-sdk-go-v2/config
go get github.com/aws/aws-sdk-go-v2/credentials
go get github.com/aws/aws-sdk-go-v2/service/s3

Before we continue there is few things which we must know:

  • object used in PutObject method must implement io.Seeker interface, this interface is implemented for example by bytes.Reader or os.File,
  • we use static credentials because we do not use native AWS configuration. ACCESS_KEY and SECRET_KEY are visible in your Vultr Object Storage panel. As SESSION_TOKEN you may use random string.
  • we define custom endpoint resolver,
  • we use path style for virtual hosting of bucket

Next we can prepare our function which upload file to Vultr Object Storage Bucket:

package vultrs3objectfunc UploadFile(file *bytes.Reader, dst string) { 
region := “us-east-1”
credentials := credentials.NewStaticCredentialsProvider(
"ACCESS_KEY",
"SECRET_KEY",
"SESSION_TOKEN",
)

endpoint := aws.Endpoint{
PartitionID: "aws",
URL: "https://ewr1.vultrobjects.com",
SigningRegion: region
}

endpointResolver := aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
return endpoint, nil
}
)

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCredentialsProvider(credentials),
config.WithRegion(region),
config.WithEndpointResolver(endpointResolver),
)

if err != nil {
return err
}

uploadObj := &s3.PutObjectInput{
Bucket: aws.String("MY_BUCKET_NAME"),
Key: aws.String(dst),
Body: file,
}

config := s3.NewFromConfig(cfg, func(o *s3.Options) { o.UsePathStyle = true })
_, err = config.PutObject(context.TODO(), uploadObj)

if err != nil {
return err
}

return nil
}

I hope this code will save your time to integrate Golang AWS SDK v2 with Vultr Object Storage.

--

--

Paweł Grzesiecki
0 Followers

I’m a staunch believer in the proof of concept methodology and simple but effective solutions. Worked with NodeJS, PHP, Python, GoLang and Elixir.