gocloud.devgocloud.dev/blob/gcsblob Index | Examples | Files

package gcsblob

import "gocloud.dev/blob/gcsblob"

Package gcsblob provides a blob implementation that uses GCS. Use OpenBucket to construct a *blob.Bucket.

Open URLs

For blob.Open URLs, gcsblob registers for the scheme "gs"; URLs start with "gs://".

The URL's Host is used as the bucket name. The following query options are supported:

Example URL:

gs://mybucket

As

gcsblob exposes the following types for As:

Example

Code:play 

package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/blob/gcsblob"
	"gocloud.dev/gcp"
	"golang.org/x/oauth2/google"
)

// jsonCreds is a fake GCP JSON credentials file.
const jsonCreds = `
{
  "type": "service_account",
  "project_id": "my-project-id"
}
`

func main() {
	ctx := context.Background()

	// Get GCP credentials.
	// Here we use a fake JSON credentials file, but you could also use
	// gcp.DefaultCredentials(ctx) to use the default GCP credentials from
	// the environment.
	// See https://cloud.google.com/docs/authentication/production
	// for more info on alternatives.
	creds, err := google.CredentialsFromJSON(ctx, []byte(jsonCreds))
	if err != nil {
		log.Fatal(err)
	}

	// Create an HTTP client.
	// This example uses the default HTTP transport and the credentials created
	// above.
	client, err := gcp.NewHTTPClient(gcp.DefaultTransport(), gcp.CredentialsTokenSource(creds))
	if err != nil {
		return
	}

	// Create a *blob.Bucket.
	b, err := gcsblob.OpenBucket(ctx, client, "my-bucket", nil)
	if err != nil {
		log.Fatal(err)
	}
	_, err = b.ReadAll(ctx, "my-key")
	if err != nil {
		// This is expected due to the fake credentials we used above.
		fmt.Println("ReadAll failed due to invalid credentials")
	}

}

Output:

ReadAll failed due to invalid credentials
Example (Open)

Code:play 

package main

import (
	"context"

	"gocloud.dev/blob"
)

func main() {
	_, _ = blob.Open(context.Background(), "gs://my-bucket")

}

Index

Examples

Functions

func OpenBucket

func OpenBucket(ctx context.Context, client *gcp.HTTPClient, bucketName string, opts *Options) (*blob.Bucket, error)

OpenBucket returns a *blob.Bucket backed by GCS. See the package documentation for an example.

Types

type Options

type Options struct {
	// GoogleAccessID represents the authorizer for SignedURL.
	// Required to use SignedURL.
	// See https://godoc.org/cloud.google.com/go/storage#SignedURLOptions.
	GoogleAccessID string

	// PrivateKey is the Google service account private key.
	// Exactly one of PrivateKey or SignBytes must be non-nil to use SignedURL.
	// See https://godoc.org/cloud.google.com/go/storage#SignedURLOptions.
	PrivateKey []byte

	// SignBytes is a function for implementing custom signing.
	// Exactly one of PrivateKey or SignBytes must be non-nil to use SignedURL.
	// See https://godoc.org/cloud.google.com/go/storage#SignedURLOptions.
	SignBytes func([]byte) ([]byte, error)
}

Options sets options for constructing a *blob.Bucket backed by GCS.

Source Files

gcsblob.go

Version
v0.9.0
Published
Jan 15, 2019
Platform
windows/amd64
Imports
17 packages
Last checked
3 hours ago

Tools for package owners.