Go

Last update:2025-08-18 15:54:22

This Go SDK is developed on top of the official API provided by CDNetworks Object Storage (OS).

Version Information

Current version: v1.0.1

Requirements

Requires Go 1.7 or above

Install

Run the following command to install the SDK
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) Specifies the maximum number of retries for interface requests. The default value is 3. 1, 5
WithProxyUrl(proxyUrl string) Configure HTTP proxy. N/A
WithHttpTransport(transport *http.Transport) Configure custom Transport. Default
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.Please use the regionName displayed in the console’s space overview. For detailed instructions, refer to Obtain Domains. Configure according to regionName
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

Feature Name
List buckets ListBuckets
Check if a bucket exists HeadBucket
Set bucket lifecycle SetBucketLifecycleConfiguration
Get bucket lifecycle GetBucketLifecycleConfiguration
Delete bucket lifecycle DeleteBucketLifecycleConfiguration
List objects ListObjects
List objects (v2) ListObjectV2
List multipart uploads ListMultipartUploads
Delete an object DeleteObject
Batch delete objects DeleteObjects
Restore archived objects RestoreObject
Initiate multipart upload InitiateMultipartUpload
Upload a part UploadPart
Copy a part CopyPart
List uploaded parts ListParts
Abort multipart upload AbortMultipartUpload
Upload an object PutObject
Upload a file PutFile
Check if an object exists HeadObject
Get object metadata GetObjectMetadata
Download an object GetObject
Get object media information GetAvinfo

Samples

Sample Description Samples
Concurrent multipart copy of large objects concurrent_copy_part_sample.go
Concurrent multipart download of large objects concurrent_download_object_sample.go
Multipart upload of large objects multipart_upload_sample.go
Create a folder create_folder_sample.go
Batch delete objects delete_objects_sample.go
Download a file download_sample.go
List objects in a bucket list_objects_sample.go
List objects in a folder list_objects_in_folder_sample.go
Object metadata operations object_meta_sample.go
Basic object operations object_operations_sample.go
Simple multipart upload simple_multipart_upload_sample.go
Temporary signature operations 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!