文件存储 NAS Go SDK 支持通过 Session 的方式来初始化客户端。通过 Session 方式创建的文件存储 NAS 客户端,底层会实现 HTTP/HTTPS 资源(连接或请求)的复用。当需要初始化多个客户端进行操作时,通过 Session 方式创建可以提升客户端的性能。本文介绍如何通过 Session 的方式来初始化客户端。
将 AccessKey 硬编码到业务代码中会带来一定的安全风险,因此我们用配置环境变量的方法管理 AccessKey。
export VOLCENGINE_ACCESS_KEY_ID=<Your AccessKey ID> export VOLCENGINE_ACCESS_KEY_SECRET=<Your AccessKey Secret>
VOLCENGINE_ACCESS_KEY_ID
和VOLCENGINE_ACCESS_KEY_SECRET
,并写入您的 AccessKey ID 和 AccessKey Secret。使用如下代码初始化客户端。
package main import ( "fmt" "os" "github.com/volcengine/volcengine-go-sdk/service/filenas" "github.com/volcengine/volcengine-go-sdk/volcengine" "github.com/volcengine/volcengine-go-sdk/volcengine/credentials" "github.com/volcengine/volcengine-go-sdk/volcengine/session" ) var client *filenas.FILENAS func initClient() { var ( ak string sk string region string config *volcengine.Config sess *session.Session err error ) ak = os.Getenv("VOLCENGINE_ACCESS_KEY_ID") sk = os.Getenv("VOLCENGINE_ACCESS_KEY_SECRET") region = "Your Region" config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).WithRegion(region) sess, err = session.NewSession(config) if err != nil { fmt.Printf("Failed to create session, err: %v\n", err) os.Exit(1) } client = filenas.New(sess) }