SDK overview
Preparation
Pub/Sub currently supports Go SDK. To use Go SDK, complete the following prerequisites.
Get access key
To obtain user authentication token (API authentication token), first get an access key and then retrieve access key ID and secret access key.
You can only access resources within the project associated with issued access key.
-
Create an access key for user credentials in kakao cloud console > User profile > Access key tab.
-
After issuing user Access key, refer to Prepare API usage to retrieve Access key ID and secret access key.
SDK requirements
Use Go SDK
Ensure the following requirements are met to use Go SDK.
SDK specifications differ between kr-central-1 and kr-central-2 regions. Download the package for the respective region.
-
Download Go package (1.19 or later).
Download Go packagewget https://objectstorage.kr-central-2.kakaocloud.com/v1/e9130193fc734337b2b0c1da50e44395/pubsub-sdk/go/v1.0.0/pubsub.tgz
tar -xf pubsub.tgz
cd {your-project}
go mod edit -require github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go@v1.0.0
go mod edit -replace github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go@v1.0.0={pubsub-sdk-path}go.mod file resultsmodule ...
require (
github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go v1.0.0
)
replace github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go v1.0.0 => {pubsub-sdk-path} -
Refer to Get access key to generate credentials. By default, the SDK attempts to authenticate using Access keys. Authentication uses the
CredentialsProvider
type, typically configured withDefaultCredentialsProvider
as shown below:Example using credentialsaccessKey := pubsub.AccessKey{CredentialID: "credentialID", CredentialSecret: "credentialSecret"}
opts := []option.ClientOption{
option.WithAccessKey(accessKey),
}
client, err := pubsub.NewClient(ctx, domainID, projectID, opts...)
You can only access resources within the project associated with issued access key.
When calling APIs, set domain and project to match the issued access key's information.
Client
Use client objects to manage topics and subscriptions, send messages, and receive messages.
You must release resources like threads used by the client object by calling the close()
method.
Configure custom settings
When creating a client object, you can customize values such as authentication and endpoint settings.
The default endpoint is set to the kr-central-2 region's endpoint. Change the endpoint when using kr-central-1.
import (
"context"
"fmt"
"github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go"
)
func test(domainID, projectID, name string) error {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, domainID, projectID)
if err != nil {
return fmt.Errorf("pubsub.NewClient: %v", err)
}
// do something
err = client.Close()
return err
}
opts := []option.ClientOption{
option.WithEndpoint("{endpoint-url}:443"),
}
client, err := pubsub.NewClient(ctx, domainID, projectID, opts...)