简体   繁体   中英

GCP: Bucket Creation in Go

I'm trying to create a bucket in GCP using Go, I tried this create method, that creates a bucket in Go. Here's the go playground of the same. When I try to run it, it just says timeout running go build and Go Build Failed . I know it's trying to download some packages, but not sure why it is getting timed out while downloading the package?. I'm new to Go and wanted to test out how bucket creation works in go. Am I missing something?.

Log in:

gcloud auth application-default login

Create this create-bucket.go file and replace the project ID with your project 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)
}

Run the go file that you created in the previous step:

go run create-bucket.go

You'll find the go-new-bucket under the GCP storage menu :

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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