/ibm-cos-sdk-go

ibm-cos-sdk-go

Primary LanguageGoApache License 2.0Apache-2.0

IBM Cloud Object Storage - Go SDK

This package allows Go developers to write software that interacts with IBM Cloud Object Storage. It is a fork of the AWS SDK for Go library and can stand as a drop-in replacement if the application needs to connect to object storage using an S3-like API and does not make use of other AWS services.

Documentation

For release notes, see the CHANGELOG.

Quick start

You'll need:

  • An instance of COS.
  • An API key from IBM Cloud Identity and Access Management with at least Writer permissions.
  • The ID of the instance of COS that you are working with.
  • Token acquisition endpoint
  • Service endpoint

These values can be found in the IBM Cloud Console by generating a 'service credential'.

Getting the SDK

Use go get to retrieve the SDK to add it to your GOPATH workspace, or project's Go module dependencies. The SDK requires a minimum version of Go 1.10.

go get github.com/IBM/ibm-cos-sdk-go

To update the SDK use go get -u to retrieve the latest version of the SDK..

go get -u github.com/IBM/ibm-cos-sdk-go

Example code

Create a file main.go, replacing your own values for API key, instance ID, and bucket name:

package main

import (
	"fmt"

	"github.com/IBM/ibm-cos-sdk-go/aws"
	"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"
)

const (
	apiKey            = "<API_KEY>"
	serviceInstanceID = "<RESOURCE_INSTANCE_ID>"
	authEndpoint      = "https://iam.bluemix.net/oidc/token"
	serviceEndpoint   = "https://s3-api.us-geo.objectstorage.softlayer.net"
)

func main() {

	newBucket := "new-bucketee"
	newColdBucket := "new-cold-bucketee"

	conf := aws.NewConfig().
		WithEndpoint(serviceEndpoint).
		WithCredentials(ibmiam.NewStaticCredentials(aws.NewConfig(),
			authEndpoint, apiKey, serviceInstanceID)).
		WithS3ForcePathStyle(true)

	sess := session.Must(session.NewSession())
	client := s3.New(sess, conf)

	input := &s3.CreateBucketInput{
		Bucket: aws.String(newBucket),
	}
	client.CreateBucket(input)

	input2 := &s3.CreateBucketInput{
		Bucket: aws.String(newColdBucket),
		CreateBucketConfiguration: &s3.CreateBucketConfiguration{
			LocationConstraint: aws.String("us-cold"),
		},
	}
	client.CreateBucket(input2)

	d, _ := client.ListBuckets(&s3.ListBucketsInput{})
	fmt.Println(d)
}

From the command line, run go run main.go. You should see a list of your buckets.

Getting Help

Feel free to use GitHub issues for tracking bugs and feature requests, but for help please use one of the following resources:

License

This SDK is distributed under the Apache License, Version 2.0, see LICENSE.txt and NOTICE.txt for more information.