Go

最終更新日:2025-08-18 15:54:22

このGo SDKは、CDNetworks Object Storage(OS)が提供する公式APIの上に開発されています。

バージョン情報

現在のバージョン:v1.0.1

必要条件

Go 1.7 以上が必要です

インストール

以下のコマンドを実行してSDKをインストールします。
go get -u github.com/CDNetworks-Object-Storage/wcs-go-sdk-v2/wos

初期化

WosClient(WOSクライアント)は、CDNetworks Object Storageへのアクセス用Goクライアントです。呼び出し元がWOSサービスとやり取りし、バケットやオブジェクトなどのリソースを管理・操作するための各種インターフェースを提供します。
WOS Go SDKを利用してリクエストを送信するには、WosClientインスタンスを初期化し、必要に応じてクライアント設定パラメータを調整してください。

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 {
    // wosClientを利用してObject Storageへアクセス
    // ...
 
    // wosClientをクローズ
    wosClient.Close()
} else {
    // API応答からエラーコードおよびメッセージの解析
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

設定

クライアントを作成する際、パラメータを指定してさまざまなクライアント設定を行うことができます。例えば、レスポンスヘッダー取得のタイムアウトを30秒に設定できます。

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

パラメータ

メソッド 説明 推奨値
WithSignature(signature SignatureType) 認証署名方式。デフォルトはWOS認証(wos.SignatureWos)。aws-v2(wos.SignatureV2)やaws-v4(wos.SignatureV4)も設定可能。 N/A
WithSslVerifyAndPemCerts(sslVerify bool, pemCerts []byte) サーバー証明書検証のパラメータを設定。デフォルトは検証しません。 N/A
WithHeaderTimeout(headerTimeout int) レスポンスヘッダー取得のタイムアウトを設定(秒)。デフォルトは60秒。 10, 60
WithMaxConnections(maxIdleConns int) HTTPアイドル接続の最大数。デフォルトは1000。 N/A
WithConnectTimeout(connectTimeout int) HTTP/HTTPS接続確立のタイムアウト(秒)を設定。デフォルトは60秒。 10, 60
WithSocketTimeout(socketTimeout int) データ読み書きのタイムアウト(秒)。デフォルトは60秒。 10, 60
WithIdleConnTimeout(idleConnTimeout int) コネクションプールのアイドルHTTP接続のタイムアウト(秒)。デフォルトは30秒。 デフォルト
WithMaxRetryCount(maxRetryCount int) インターフェースリクエストの最大リトライ回数。デフォルトは3回。 1, 5
WithProxyUrl(proxyUrl string) HTTPプロキシの設定。 N/A
WithHttpTransport(transport *http.Transport) カスタムTransportの設定。 デフォルト
WithRequestContext(ctx context.Context) 各HTTPリクエスト毎のcontext設定。 N/A
WithMaxRedirectCount(maxRedirectCount int) HTTP/HTTPSリクエストの最大リダイレクト回数。デフォルトは3回。 1, 5
WithRegion(region string) S3のリージョン設定。consoleのスペース概要に表示されるregionNameを使用してください。詳細はObtain Domainsを参照。 regionNameに従って設定
WithPathStyle(pathStyle boolean) サービスアクセス時にpath-styleまたはvirtual-hosted-style URLを使用するかの設定。無効の場合、bucketName.endpoint形式のURLを使用、有効の場合はendpoint/bucketName形式を使用。デフォルトは無効。 N/A

クイックスタート

バケット一覧取得

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 {
    // API応答からエラーコードおよびメッセージの解析
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

オブジェクト一覧取得

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 {
    // API応答からエラーコードおよびメッセージの解析
    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 {
    // API応答からエラーコードおよびメッセージの解析
    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 {
    // API応答からエラーコードおよびメッセージの解析
    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 {
    // API応答からエラーコードおよびメッセージの解析
    if wosError, ok := err.(wos.WosError); ok {
	fmt.Println(wosError.StatusCode)
	fmt.Println(wosError.Code)
	fmt.Println(wosError.Message)
    } else {
	fmt.Println(err)
    }
}

マルチパートアップロード

シンプルなマルチパートアップロード

example/simple_multipart_upload_sample.go をご参照ください。

並列マルチパートアップロード

example/multipart_upload_sample.go をご参照ください。

機能一覧

wos_go_sample.go のサンプルを参照してください

機能 名称
バケット一覧取得 ListBuckets
バケット存在確認 HeadBucket
バケットライフサイクル設定 SetBucketLifecycleConfiguration
バケットライフサイクル取得 GetBucketLifecycleConfiguration
バケットライフサイクル削除 DeleteBucketLifecycleConfiguration
オブジェクト一覧取得 ListObjects
オブジェクト一覧取得(v2) ListObjectV2
マルチパートアップロード一覧取得 ListMultipartUploads
オブジェクト削除 DeleteObject
オブジェクト一括削除 DeleteObjects
アーカイブオブジェクトの復元 RestoreObject
マルチパートアップロード開始 InitiateMultipartUpload
パートのアップロード UploadPart
パートのコピー CopyPart
アップロード済みパート一覧取得 ListParts
マルチパートアップロード中止 AbortMultipartUpload
オブジェクトのアップロード PutObject
ファイルのアップロード PutFile
オブジェクト存在確認 HeadObject
オブジェクトメタデータ取得 GetObjectMetadata
オブジェクトのダウンロード GetObject
オブジェクトメディア情報取得 GetAvinfo

サンプル

サンプル内容 サンプル名
大きなオブジェクトの並列マルチパートコピー 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