Go

Last update:2023-08-14 15:26:46

Version

Current version: v1.0.1

Requirements

Go 1.7 or above

Install

go get -u github.com/CDNetworks-Object-Storage/wcs-go-sdk-v2/wos

Initialization

The WosClient (WOS Client) is a Go client for accessing CDNetwork Object Storage. It provides a range of interfaces for callers to interact with WOS services, managing and operating resources such as buckets and objects on the Object Storage.
To make requests using the WOS Go SDK, you need to initialize a WosClient instance and adjust client configuration parameters as needed.

Create a wosClient

var ak = "*** Provide your Access Key ***" 
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
var regionName = "your-regionName"
 
wosClient, err := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
if err == nil {
    // Use wosClient to access Object Storage
    // ...
 
    // Close wosClient
    wosClient.Close()
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

Configurations

When creating the client, you can specify different client configurations using parameters. For example, set the timeout for retrieving response headers to 30 seconds.

wosClient, err := wos.New(ak, sk, endpoint, wos.WithHeaderTimeout(30))

Parameters

method description recommended
WithSignature(signature SignatureType) Authentication signature type. Default is WOS authentication (wos.SignatureWos). Can also be configured as aws-v2 (wos.SignatureV2) or aws-v4 (wos.SignatureV4). N/A
WithSslVerifyAndPemCerts(sslVerify bool, pemCerts []byte) Configure parameters to validate server certificates. Default is no validation. N/A
WithHeaderTimeout(headerTimeout int) Configure timeout for retrieving response headers. Default is 60 seconds. 10, 60
WithMaxConnections(maxIdleConns int) Configure the maximum number of idle HTTP connections. Default is 1000. N/A
WithConnectTimeout(connectTimeout int) Configure timeout for establishing HTTP/HTTPS connections (in seconds). Default is 60 seconds. 10, 60
WithSocketTimeout(socketTimeout int) Configure timeout for reading/writing data (in seconds). Default is 60 seconds. 10, 60
WithIdleConnTimeout(idleConnTimeout int) Configure timeout for idle HTTP connections in the connection pool (in seconds). Default is 30 seconds. Default
WithMaxRetryCount(maxRetryCount int) Configure timeout for idle HTTP connections in the connection pool (in seconds). Default is 30 seconds. 1, 5
WithProxyUrl(proxyUrl string) Configure HTTP proxy. N/A
WithHttpTransport(transport *http.Transport) Configure custom Transport. N/A
WithRequestContext(ctx context.Context) Configure the context for each HTTP request. N/A
WithMaxRedirectCount(maxRedirectCount int) Configure the maximum number of redirects for HTTP/HTTPS requests. Default is 3 times. 1, 5
WithRegion(region string) Set the region of S3 N/A
WithPathStyle(pathStyle boolean) Whether to use path-style or virtual-hosted-style URLs for accessing the service. When disabled, use the bucketName.endpoint format URL for service access; when enabled, use the endpoint/bucketName format URL for service access. It is disabled by default. N/A

Quick Start

List Bucket

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
 
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
 
input := &wos.ListBucketsInput{}
input.QueryLocation = true
output, err := wosClient.ListBuckets(input)
if err == nil {
    fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
    fmt.Printf("Owner.DisplayName:%s, Owner.ID:%s\n", output.Owner.DisplayName, output.Owner.ID)
    for index, val := range output.Buckets {
        fmt.Printf("Bucket[%d]-Name:%s,CreationDate:%s,EndPoint:%s,Region:%s\n", index, val.Name, val.CreationDate, val.Endpoint, val.Region)
    }
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

List Objects

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
 
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
 
input := &wos.ListObjectsInput{}
input.Bucket = bucketName
//  input.Prefix = "src/"
output, err := wosClient.ListObjects(input)
if err == nil {
    fmt.Printf("StatusCode:%d, RequestId:%s,OwnerId:%s,OwnerName:%s,\n", output.StatusCode, output.RequestId, output.Owner.ID, output.Owner.DisplayName)
    for index, val := range output.Contents {
        fmt.Printf("Content[%d]-ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
            index, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
    }
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

Put Object

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
 
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
 
input := &wos.PutObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
input.Metadata = map[string]string{"meta": "value"}
input.Body = strings.NewReader("Hello WOS")
output, err := wosClient.PutObject(input)
if err == nil {
    fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
    fmt.Printf("ETag:%s", output.ETag)
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

Get Object

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
 
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
 
input := &wos.GetObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
output, err := wosClient.GetObject(input)
if err == nil {
    defer output.Body.Close()
    fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
    fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n",
        output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified)
    p := make([]byte, 1024)
    var readErr error
    var readCount int
    for {
        readCount, readErr = output.Body.Read(p)
        if readCount > 0 {
            fmt.Printf("%s", p[:readCount])
        }
        if readErr != nil {
            break
        }
    }
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

Delete Object

var ak = "*** Provide your Access Key ***"
var sk = "*** Provide your Secret Key ***"
var endpoint = "https://your-endpoint"
 
wosClient, _ := wos.New(ak, sk, endpoint, wos.WithRegion("your-region"))
 
input := &wos.DeleteObjectInput{}
input.Bucket = bucketName
input.Key = objectKey
output, err := wosClient.DeleteObject(input)
if err == nil {
    fmt.Printf("StatusCode:%d, RequestId:%s\n", output.StatusCode, output.RequestId)
} else {
    // Parse error code and message from the API response
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

Multipart Upload

Simple Multipart Upload

Refer to example/simple_multipart_upload_sample.go

Concurrent Multipart Upload

Refer to example/multipart_upload_sample.go

Functionalities List

See examples at wos_go_sample.go

Name
ListBuckets
HeadBucket
SetBucketLifecycleConfiguration
GetBucketLifecycleConfiguration
deleteBucketLifecycleConfiguration
ListObjects
ListObjectV2
ListMultipartUploads
DeleteObject
DeleteObjects
RestoreObject
InitiateMultipartUpload
UploadPart
CopyPart
ListParts
AbortMultipartUpload
PutObject
PutFile
HeadObject
GetObjectMetadata
GetObject
GetAvinfo

Samples

Samples
concurrent_copy_part_sample.go
concurrent_download_object_sample.go
multipart_upload_sample.go
create_folder_sample.go
delete_objects_sample.go
download_sample.go
list_objects_sample.go
list_objects_in_folder_sample.go
object_meta_sample.go
object_operations_sample.go
simple_multipart_upload_sample.go
temporary_signature_sample.go
Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!