Skip to content

Commit

Permalink
examples: improve go sdk usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jul 20, 2023
1 parent 1633eff commit ae2d59d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
21 changes: 9 additions & 12 deletions examples/rrsa/go-sdk/main.go
Expand Up @@ -20,9 +20,12 @@ const (

func testOpenAPISDK() {
// 两种方法都可以
cred := newCredential()
cred, err := newCredential()
// or
// cred := newOidcCredential()
// cred, err := newOidcCredential()
if err != nil {
panic(err)
}

config := &openapi.Config{Credential: cred}
config.Endpoint = tea.String("cs.cn-hangzhou.aliyuncs.com")
Expand All @@ -41,16 +44,13 @@ func testOpenAPISDK() {
}
}

func newCredential() credentials.Credential {
func newCredential() (credentials.Credential, error) {
// https://www.alibabacloud.com/help/doc-detail/378661.html
cred, err := credentials.NewCredential(nil)
if err != nil {
panic(err)
}
return cred
return cred, err
}

func newOidcCredential() credentials.Credential {
func newOidcCredential() (credentials.Credential, error) {
// https://www.alibabacloud.com/help/doc-detail/378661.html
config := new(credentials.Config).
SetType("oidc_role_arn").
Expand All @@ -60,10 +60,7 @@ func newOidcCredential() credentials.Credential {
SetRoleSessionName("test-rrsa-oidc-token")

oidcCredential, err := credentials.NewCredential(config)
if err != nil {
panic(err)
}
return oidcCredential
return oidcCredential, err
}

func main() {
Expand Down
43 changes: 18 additions & 25 deletions examples/rrsa/oss-go-sdk/main.go
Expand Up @@ -19,9 +19,12 @@ const (

func testOSSSDK() {
// 两种方法都可以
cred := newCredential()
cred, err := newCredential()
// or
// cred := newOidcCredential()
// cred, err := newOidcCredential()
if err != nil {
panic(err)
}

provider := &OSSCredentialsProvider{cred: cred}
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "",
Expand All @@ -40,16 +43,13 @@ func testOSSSDK() {
}
}

func newCredential() credentials.Credential {
func newCredential() (credentials.Credential, error) {
// https://www.alibabacloud.com/help/doc-detail/378661.html
cred, err := credentials.NewCredential(nil)
if err != nil {
panic(err)
}
return cred
return cred, err
}

func newOidcCredential() credentials.Credential {
func newOidcCredential() (credentials.Credential, error) {
// https://www.alibabacloud.com/help/doc-detail/378661.html
config := new(credentials.Config).
SetType("oidc_role_arn").
Expand All @@ -59,49 +59,42 @@ func newOidcCredential() credentials.Credential {
SetRoleSessionName("test-rrsa-oidc-token")

oidcCredential, err := credentials.NewCredential(config)
if err != nil {
panic(err)
}
return oidcCredential
return oidcCredential, err
}

type OSSCredentials struct {
teaCred credentials.Credential
type OSSCredentialsProvider struct {
cred credentials.Credential
}

func (cred *OSSCredentials) GetAccessKeyID() string {
value, err := cred.teaCred.GetAccessKeyId()
func (p *OSSCredentialsProvider) GetAccessKeyID() string {
value, err := p.cred.GetAccessKeyId()
if err != nil {
log.Printf("get access key id failed: %+v", err)
return ""
}
return tea.StringValue(value)
}

func (cred *OSSCredentials) GetAccessKeySecret() string {
value, err := cred.teaCred.GetAccessKeySecret()
func (p *OSSCredentialsProvider) GetAccessKeySecret() string {
value, err := p.cred.GetAccessKeySecret()
if err != nil {
log.Printf("get access key secret failed: %+v", err)
return ""
}
return tea.StringValue(value)
}

func (cred *OSSCredentials) GetSecurityToken() string {
value, err := cred.teaCred.GetSecurityToken()
func (p *OSSCredentialsProvider) GetSecurityToken() string {
value, err := p.cred.GetSecurityToken()
if err != nil {
log.Printf("get access security token failed: %+v", err)
return ""
}
return tea.StringValue(value)
}

type OSSCredentialsProvider struct {
cred credentials.Credential
}

func (p *OSSCredentialsProvider) GetCredentials() oss.Credentials {
return &OSSCredentials{teaCred: p.cred}
return p
}

func main() {
Expand Down

0 comments on commit ae2d59d

Please sign in to comment.