Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Last active March 11, 2020 16:35
Show Gist options
  • Save owainlewis/2a8b54b94836eded67dfe037608c32a9 to your computer and use it in GitHub Desktop.
Save owainlewis/2a8b54b94836eded67dfe037608c32a9 to your computer and use it in GitHub Desktop.
OCI S3 API with Golang

OCI + S3

Oracle Cloud Infrastructure offers an S3 compatible API for object storage.

This example will show you how to use it with the AWS Go SDK

Configuration

Endpoint

%s.compat.objectstorage.%s.oraclecloud.com (tenancy, region)

Configure the client with your access key and secret access key

func createClient(tenancy, region, accessKey, secretAccessKey string) *s3.S3 {
	endpoint := fmt.Sprintf("%s.compat.objectstorage.%s.oraclecloud.com", tenancy, region)
	s3Config := &aws.Config{
		Credentials:      credentials.NewStaticCredentials(accessKey, secretAccessKey, ""),
		Endpoint:         aws.String(endpoint),
		Region:           aws.String(region),
		S3ForcePathStyle: aws.Bool(true),
	}
	newSession := session.New(s3Config)

	return s3.New(newSession)
}

func main() {
	accessKey := "ocid1.credential.oc1...."
	secretAccessKey := "M4MivGH9Z0ILEgwpoz60rmg..."

	s3Client := createClient("spinnaker", "eu-frankfurt-1", accessKey, secretAccessKey)
}

Create a bucket

file, err := os.Open("README.md")
if err != nil {
	return
}

_, err = s3Client.PutObject(&s3.PutObjectInput{
	// io.ReadSeeker (ReadSeeker is the interface that groups the basic Read and Seek methods)
	Body:   file,
	Bucket: aws.String("backups"),
	Key:    aws.String("hello"),
})

if err != nil {
	fmt.Printf("Failed to upload: %s", err.Error())
	return
}

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment