Created
January 28, 2020 14:18
-
-
Save baranov1ch/4d3c412d9f3fcd5af58b5b72cb6d2564 to your computer and use it in GitHub Desktop.
yc sdk with service account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"context" | |
"log" | |
ycsdk "github.com/yandex-cloud/go-sdk" | |
"github.com/yandex-cloud/go-sdk/iamkey" | |
) | |
func main() { | |
ctx := context.Background() | |
sdk, err := ycsdk.Build(ctx, ycsdk.Config{ | |
Credentials: ycsdk.InstanceServiceAccount(), // если запускается с VMки, с назначенным SA | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Используя статический ключ SA | |
// Статический ключ для SA можно получить через yc или API: | |
// https://github.com/yandex-cloud/cloudapi/blob/master/yandex/cloud/iam/v1/key_service.proto#L30 | |
key, err := iamkey.ReadFromJSONFile("path-to-service-account-key-file") | |
if err != nil { | |
log.Fatal(err) | |
} | |
saCreds, err := ycsdk.ServiceAccountKey(key) | |
if err != nil { | |
log.Fatal(err) | |
} | |
sdk, err = ycsdk.Build(ctx, ycsdk.Config{ | |
Credentials: saCreds, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// ... hack-hack-hack | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment