Setup Vultr Object Storage with AWS SDK v2 and Golang

Setup Vultr Object Storage with AWS SDK v2 and Golang

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 are few things that we must know:

  • an object used in the 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 vultrs3object
func 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 you time to integrate Golang AWS SDK v2 with Vultr Object Storage.

Did you find this article valuable?

Support pgrzesiecki by becoming a sponsor. Any amount is appreciated!