[英]GCP: Bucket Creation in Go
登录:
gcloud auth application-default login
创建这个create-bucket.go
文件并将项目 ID 替换为您的项目 ID:
// Sample storage-quickstart creates a Google Cloud Storage bucket.
package main
import (
"context"
"fmt"
"log"
"time"
"cloud.google.com/go/storage"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "<YOUR-PROJECT-ID>"
// Creates a client.
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the name for the new bucket.
bucketName := "go-new-bucket"
// Creates a Bucket instance.
bucket := client.Bucket(bucketName)
// Creates the new bucket.
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
if err := bucket.Create(ctx, projectID, nil); err != nil {
log.Fatalf("Failed to create bucket: %v", err)
}
fmt.Printf("Bucket %v created.\n", bucketName)
}
运行您在上一步中创建的 go 文件:
go run create-bucket.go
您将在GCP 存储菜单下找到go-new-bucket
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.