Accessing services with Aws-SDK-Go-v2

This article describes how to access the sufy object storage service with aws-sdk-go-v2

The code is based on the following package versions

github.com/aws/aws-sdk-go-v2 v1.24.1
github.com/aws/aws-sdk-go-v2/config v1.26.6
github.com/aws/aws-sdk-go-v2/credentials v1.16.16
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.1
package main

import (
	"context"
	"fmt"
	"log"

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

// The region id can be viewed in the bucket details
const s3Endpoint = "https://{bucket-name}.mos.{region-id}.sufybkt.com"

func main() {
	// Please go to the console to obtain the accessKey secretKey
	s3AccessKey := "your-access-key"
	s3SecretKey := "your-secret-key"

	// create S3 client
	s3Client, err := getS3Client(s3AccessKey, s3SecretKey)
	if err != nil {
		log.Println("get s3 client failed, err=", err)
		return
	}
	log.Println("get s3 client success", s3Client)
}

func getS3Client(key, secret string) (*s3.Client, error) {
	customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		if service == "S3" {
			return aws.Endpoint{
				URL: s3Endpoint,
			}, nil
		}
		return aws.Endpoint{}, fmt.Errorf("unknown service requested")
	})

	customProvider := credentials.NewStaticCredentialsProvider(key, secret, "")
	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(customProvider), config.WithEndpointResolverWithOptions(customResolver))
	if err != nil {
		return nil, err
	}
        cfg.Region = "cn-east-1"
	s3client := s3.NewFromConfig(cfg)
	return s3client, nil
}

List Objects in Bucket

package main

import (
	"context"
	"fmt"
	"log"

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

// The region id can be viewed in the bucket details
const s3Endpoint = "https://{bucket-name}.mos.{region-id}.sufybkt.com"

func main() {
	// Please go to the console to obtain the accessKey secretKey
	s3AccessKey := "your-access-key"
	s3SecretKey := "your-secret-key"

	s3Client, err := getS3Client(s3AccessKey, s3SecretKey)
	if err != nil {
		log.Println("get s3 client failed, err=", err)
		return
	}

	bucket := "your-bucket-name"

	// list objects
	listObjsResponse, err := s3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
		Bucket:    aws.String(bucket),
		Delimiter: aws.String("/"),
		MaxKeys:   aws.Int32(50),
	})
	if err != nil {
		log.Println("list objects failed, err=", err)
		return
	}

	// print objects
	for _, object := range listObjsResponse.Contents {
		log.Printf("object key=%v", *object.Key)
	}

	log.Println("list objects success")
}

func getS3Client(key, secret string) (*s3.Client, error) {
	customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		if service == "S3" {
			return aws.Endpoint{
				URL: s3Endpoint,
			}, nil
		}
		return aws.Endpoint{}, fmt.Errorf("unknown service requested")
	})

	customProvider := credentials.NewStaticCredentialsProvider(key, secret, "")
	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(customProvider), config.WithEndpointResolverWithOptions(customResolver))
	if err != nil {
		return nil, err
	}
        cfg.Region = "cn-east-1"
	s3client := s3.NewFromConfig(cfg)
	return s3client, nil
}

Get a download link for time-sensitive pre-signed documents

package main

import (
	"context"
	"fmt"
	"log"
	"time"

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

// The region id can be viewed in the bucket details
const s3Endpoint = "https://{bucket-name}.mos.{region-id}.sufybkt.com"

func main() {
	// Please go to the console to obtain the accessKey secretKey
	s3AccessKey := "your-access-key"
	s3SecretKey := "your-secret-key"

	s3Client, err := getS3Client(s3AccessKey, s3SecretKey)
	if err != nil {
		log.Println("get s3 client failed, err=", err)
		return
	}

	bucket := "your-bucket-name"   // eg:test
	objectKey := "your-object-key" // eg:test.txt

	// new presign client
	preSignClient := s3.NewPresignClient(s3Client)

	preSignedRequest, _ := preSignClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{
		Bucket: aws.String(bucket),
		Key:    aws.String(objectKey),
	}, func(presignOptions *s3.PresignOptions) {
		presignOptions.Expires = time.Hour
	})

	preSignedUrl := preSignedRequest.URL
	log.Printf("get preSigned url success, preSignedUrl=%v", preSignedUrl)
}

func getS3Client(key, secret string) (*s3.Client, error) {
	customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		if service == "S3" {
			return aws.Endpoint{
				URL: s3Endpoint,
			}, nil
		}
		return aws.Endpoint{}, fmt.Errorf("unknown service requested")
	})

	customProvider := credentials.NewStaticCredentialsProvider(key, secret, "")
	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(customProvider), config.WithEndpointResolverWithOptions(customResolver))
	if err != nil {
		return nil, err
	}
        cfg.Region = "cn-east-1"
	s3client := s3.NewFromConfig(cfg)
	return s3client, nil
}