Last active
December 28, 2021 12:08
-
-
Save lucasjellema/3f9c850c0530d06c9b8b23f07eecea46 to your computer and use it in GitHub Desktop.
OCI Go SDK ObjectStorag explorations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
"time" | |
"github.com/oracle/oci-go-sdk/v54/common" | |
"github.com/oracle/oci-go-sdk/v54/objectstorage" | |
) | |
const tenancyOCID string = "ocid1.tenancy.oc1..aokq" | |
const userOCID string = "ocid1.user.oc1..aaaaaaa" | |
const region string = "us-ashburn-1" | |
const fingerprint string = "02:91:6c:49:d8:04:56" | |
const compartmentOCID = "ocid1.compartment.oc1..aaaaazrq" | |
func initializeConfigurationProvider() common.ConfigurationProvider { | |
privateKey := os.Getenv("pem") // set content of PEM in environment variable pem using export pem=$(cat ./ppk) with ppk a text file that contains the PEM private key00 | |
configurationProvider := common.NewRawConfigurationProvider(tenancyOCID, userOCID, region, fingerprint, string(privateKey), nil) | |
return configurationProvider | |
} | |
// fatalIfError is equivalent to Println() followed by a call to os.Exit(1) if error is not nil | |
func fatalIfError(err error) { | |
if err != nil { | |
log.Fatalln(err.Error()) | |
} | |
} | |
func getResponseStatusCode(response *http.Response) int { | |
return response.StatusCode | |
} | |
// bucketname needs to be unique within compartment. there is no concept of "child" buckets. | |
func ensureBucketExists(ctx context.Context, client objectstorage.ObjectStorageClient, namespace, name string, compartmentOCID string) { | |
req := objectstorage.GetBucketRequest{ | |
NamespaceName: &namespace, | |
BucketName: &name, | |
} | |
// verify if bucket exists | |
response, err := client.GetBucket(context.Background(), req) | |
if err != nil { | |
fatalIfError(err) | |
if 404 == response.RawResponse.StatusCode { | |
createBucket(ctx, client, namespace, name, compartmentOCID) | |
} | |
} | |
} | |
// bucketname needs to be unique within compartment. there is no concept of "child" buckets. | |
func createBucket(ctx context.Context, client objectstorage.ObjectStorageClient, namespace string, name string, compartmentOCID string) { | |
request := objectstorage.CreateBucketRequest{ | |
NamespaceName: &namespace, | |
} | |
request.CompartmentId = &compartmentOCID | |
request.Name = &name | |
request.Metadata = make(map[string]string) | |
request.PublicAccessType = objectstorage.CreateBucketDetailsPublicAccessTypeNopublicaccess | |
_, err := client.CreateBucket(ctx, request) | |
fatalIfError(err) | |
fmt.Println("Created bucket ", name) | |
} | |
func getNamespace(ctx context.Context, client objectstorage.ObjectStorageClient) string { | |
request := objectstorage.GetNamespaceRequest{} | |
r, err := client.GetNamespace(ctx, request) | |
fatalIfError(err) | |
return *r.Value | |
} | |
func putObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespace, bucketname, objectname string, objectContent, metadata map[string]string) error { | |
request := objectstorage.PutObjectRequest{ | |
NamespaceName: &namespace, | |
BucketName: &bucketname, | |
ObjectName: &objectname, | |
ContentLength: &int64(len(objectContent)), | |
PutObjectBody: ioutil.NopCloser(strings.NewReader(objectContent)), | |
OpcMeta: metadata, | |
} | |
_, err := c.PutObject(ctx, request) | |
fmt.Println("Put object ", objectname, " in bucket ", bucketname) | |
return err | |
} | |
func getObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespace string, bucketname string, objectname string) (string, error) { | |
fmt.Println("get object ", objectname) | |
request := objectstorage.GetObjectRequest{ | |
NamespaceName: &namespace, | |
BucketName: &bucketname, | |
ObjectName: &objectname, | |
} | |
response, err := c.GetObject(ctx, request) | |
// fmt.Println("get object, status code ", response.RawResponse.StatusCode) | |
// fmt.Println("content length ", *response.ContentLength) | |
// fmt.Println("content type ", *response.ContentType) | |
buf := new(strings.Builder) | |
_, err = io.Copy(buf, response.Content) | |
return buf.String(), err | |
} | |
func deleteObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespace, bucketname, objectname string) (err error) { | |
request := objectstorage.DeleteObjectRequest{ | |
NamespaceName: &namespace, | |
BucketName: &bucketname, | |
ObjectName: &objectname, | |
} | |
_, err = c.DeleteObject(ctx, request) | |
fatalIfError(err) | |
fmt.Println("Deleted object ", objectname) | |
return | |
} | |
func main() { | |
configurationProvider := initializeConfigurationProvider() | |
objectStorageClient, clerr := objectstorage.NewObjectStorageClientWithConfigurationProvider(configurationProvider) | |
fatalIfError(clerr) | |
ctx := context.Background() | |
namespace := getNamespace(ctx, objectStorageClient) | |
bucketName := "GoBucket" | |
ensureBucketExists(ctx, objectStorageClient, namespace, bucketName, compartmentOCID) | |
objectName := "FreshObject" | |
objectContent := "My Content in plain text" | |
err := putObject(ctx, objectStorageClient, namespace, bucketName, objectName, objectContent, nil) | |
fatalIfError(err) | |
// remove the object after 60 seconds - by first deferring the removal and then waiting for 8 seconds | |
defer deleteObject(ctx, objectStorageClient, namespace, bucketName, objectName) | |
fmt.Println("go get object ", objectName) | |
contents, error := getObject(ctx, objectStorageClient, namespace, bucketName, objectName) | |
fatalIfError(error) | |
fmt.Println("Object contents: ", contents) | |
fmt.Println("Go Sleep") | |
time.Sleep(60 * time.Second) | |
// when sleep is over, deferred statements are executed prior to exiting the application | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment