gcplib

package
v0.0.0-...-aa4210d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gcplib provides a client for interacting with GCP storage buckets.

Example usage:

bucket, err := NewBucket(ctx, "my-bucket")
if err != nil {
	log.Fatal(err)
}

// Upload a file to the bucket
w, err := os.Open("path/to/file")
if err != nil {
	log.Fatal(err)
}
defer w.Close()
n, err := bucket.Upload(ctx, "path/to/object", w)
if err != nil {
	log.Fatal(err)
}
log.Printf("uploaded %d bytes to %s", n, bucket.Bucket)

// Download a file from the bucket
r, err := bucket.Download(ctx, "path/to/object")
if err != nil {
	log.Fatal(err)
}
defer r.Close()
w, err = os.Create("path/to/downloaded/file")
if err != nil {
	log.Fatal(err)
}
defer w.Close()
n, err = io.Copy(w, r)
if err != nil {
	log.Fatal(err)
}
log.Printf("downloaded %d bytes from %s", n, bucket.Bucket)

// Check if a file exists in the bucket
exists, err := bucket.Exists(ctx, "path/to/object")
if err != nil {
	log.Fatal(err)
}
log.Printf("file exists in %s: %t", bucket.Bucket, exists)

// List the objects in the bucket
objects, err := bucket.List(ctx, &ListOptions{Prefix: "path/to/prefix"})
if err != nil {
	log.Fatal(err)
}
for _, o := range objects {
	log.Printf("%s/%s", bucket.Bucket, o.Name)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Bucket string
	// contains filtered or unexported fields
}

Bucket represents a GCP storage bucket

func NewBucket

func NewBucket(ctx context.Context, bucket string) (*Bucket, error)

NewBucket returns a new GCP storage bucket

func (*Bucket) Delete

func (b *Bucket) Delete(ctx context.Context, object string) error

Delete deletes a file from a GCP bucket

func (*Bucket) Download

func (b *Bucket) Download(ctx context.Context, object string, w io.Writer) (int64, error)

Download downloads a file from a GCP bucket

func (*Bucket) Exists

func (b *Bucket) Exists(ctx context.Context, object string) (bool, error)

Exists checks if a file exists in a GCP bucket

func (*Bucket) Info

func (b *Bucket) Info(ctx context.Context, object string) (*storage.ObjectAttrs, error)

Info returns information about an object without downloading it

func (*Bucket) List

func (b *Bucket) List(ctx context.Context, opts *ListOptions) ([]*storage.ObjectAttrs, error)

List lists the objects in a GCP bucket with optional filters

func (*Bucket) Upload

func (b *Bucket) Upload(ctx context.Context, object string, r io.Reader) (int64, error)

Upload uploads a file to a GCP bucket

type ListOptions

type ListOptions struct {
	Prefix     string
	Delimiter  string
	MaxResults int
}

ListOptions are options for listing objects in a GCP bucket

type PubSub

type PubSub struct {
	// contains filtered or unexported fields
}

PubSub represents a GCP Pub/Sub client

func NewPubSub

func NewPubSub(ctx context.Context, projectID string) (*PubSub, error)

NewPubSub creates a new PubSub client

func (*PubSub) Publish

func (p *PubSub) Publish(ctx context.Context, topicID string, data []byte) (string, error)

Publish publishes a message to a topic

func (*PubSub) Pull

func (p *PubSub) Pull(ctx context.Context, subscriptionID string, callback func(*pubsub.Message)) (func(), error)

Pull pulls messages from a subscription until the context is cancelled It returns a cancel function that can be called to stop pulling messages

func (*PubSub) Subscription

func (p *PubSub) Subscription(ctx context.Context, topicID, subscriptionID string) (*pubsub.Subscription, error)

Subscription creates a new subscription

func (*PubSub) Topic

func (p *PubSub) Topic(ctx context.Context, topicID string) (*pubsub.Topic, error)

Topic creates a new topic

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL