繁体   English   中英

GCP:在 Go 中创建存储桶

[英]GCP: Bucket Creation in Go

我正在尝试使用 Go 在 GCP 中创建一个存储桶,我尝试了这种创建方法,它在 Go 中创建了一个存储桶。 这是相同的go 操场 当我尝试运行它时,它只是说timeout running go buildGo Build Failed 我知道它正在尝试下载一些软件包,但不确定为什么在下载 package 时它会超时? 我是 Go 的新手,想测试在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM