func (s *COSSession) CreateBucket(bucket, locationConstraint string) (string, error) {
	_, err := s.svc.CreateBucket(&s3.CreateBucketInput{
		Bucket: aws.String(bucket),
		CreateBucketConfiguration: &s3.CreateBucketConfiguration{
			LocationConstraint: aws.String(locationConstraint),
		},
	})
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "BucketAlreadyOwnedByYou" {
			s.logger.Warn(fmt.Sprintf("bucket '%s' already exists", bucket))
			return fmt.Sprintf("bucket '%s' already exists", bucket), nil
		}
		return "", err
	}
	return "", nil
}
CreateBucket methods creates a new bucket
package backend
import (
	"fmt"
	"github.com/IBM/ibm-cos-sdk-go/aws"
	"github.com/IBM/ibm-cos-sdk-go/aws/awserr"
	"github.com/IBM/ibm-cos-sdk-go/aws/credentials"
	"github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam"
	"github.com/IBM/ibm-cos-sdk-go/aws/session"
	"github.com/IBM/ibm-cos-sdk-go/service/s3"
	"go.uber.org/zap"
	"strings"
)
type ObjectStorageCredentials struct {
	AccessKey string
	SecretKey string
	APIKey string
	ServiceInstanceID string
	IAMEndpoint string
}
type ObjectStorageSessionFactory interface {
	NewObjectStorageSession(endpoint, region string, creds *ObjectStorageCredentials, logger *zap.Logger) ObjectStorageSession
}
type ObjectStorageSession interface {
	CheckBucketAccess(bucket string) error
	CheckObjectPathExistence(bucket, objectpath string) (bool, error)
	CreateBucket(bucket, locationConstraint string) (string, error)
	DeleteBucket(bucket string) error
}
type COSSessionFactory struct{}
type s3API interface {
	HeadBucket(input *s3.HeadBucketInput) (*s3.HeadBucketOutput, error)
	CreateBucket(input *s3.CreateBucketInput) (*s3.CreateBucketOutput, error)
	ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error)
	DeleteObject(input *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error)
	DeleteBucket(input *s3.DeleteBucketInput) (*s3.DeleteBucketOutput, error)
}
type COSSession struct {
	svc    s3API
	logger *zap.Logger
}
func (s *COSSessionFactory) NewObjectStorageSession(endpoint, region string, creds *ObjectStorageCredentials, logger *zap.Logger) ObjectStorageSession {
	var sdkCreds *credentials.Credentials
	if creds.APIKey != "" {
		sdkCreds = ibmiam.NewStaticCredentials(aws.NewConfig(), creds.IAMEndpoint+"/identity/token", creds.APIKey, creds.ServiceInstanceID)
	} else {
		sdkCreds = credentials.NewStaticCredentials(creds.AccessKey, creds.SecretKey, "")
	}
	sess, _ := session.NewSession(&aws.Config{
		S3ForcePathStyle: aws.Bool(true),
		Endpoint:         aws.String(endpoint),
		Credentials:      sdkCreds,
		Region:           aws.String(region),
	})
	return &COSSession{
		svc:    s3.New(sess),
		logger: logger,
	}
}
func (s *COSSession) CheckBucketAccess(bucket string) error {
	_, err := s.svc.HeadBucket(&s3.HeadBucketInput{
		Bucket: aws.String(bucket),
	})
	return err
}
func (s *COSSession) CheckObjectPathExistence(bucket, objectpath string) (bool, error) {
	if strings.HasPrefix(objectpath, "/") {
		objectpath = strings.TrimPrefix(objectpath, "/")
	}
	if !strings.HasSuffix(objectpath, "/") {
		objectpath = objectpath + "/"
	}
	resp, err := s.svc.ListObjects(&s3.ListObjectsInput{
		Bucket:  aws.String(bucket),
		MaxKeys: aws.Int64(1),
		Prefix:  aws.String(objectpath),
	})
	if err != nil {
		return false, fmt.Errorf("cannot list bucket '%s': %v", bucket, err)
	}
	if len(resp.Contents) == 1 {
		object := *(resp.Contents[0].Key)
		if (object == objectpath) || (strings.TrimSuffix(object, "/") == objectpath) {
			return true, nil
		}
	}
	return false, nil
}