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 ¶
- type Bucket
- func (b *Bucket) Delete(ctx context.Context, object string) error
- func (b *Bucket) Download(ctx context.Context, object string, w io.Writer) (int64, error)
- func (b *Bucket) Exists(ctx context.Context, object string) (bool, error)
- func (b *Bucket) Info(ctx context.Context, object string) (*storage.ObjectAttrs, error)
- func (b *Bucket) List(ctx context.Context, opts *ListOptions) ([]*storage.ObjectAttrs, error)
- func (b *Bucket) Upload(ctx context.Context, object string, r io.Reader) (int64, error)
- type ListOptions
- type PubSub
- func (p *PubSub) Publish(ctx context.Context, topicID string, data []byte) (string, error)
- func (p *PubSub) Pull(ctx context.Context, subscriptionID string, callback func(*pubsub.Message)) (func(), error)
- func (p *PubSub) Subscription(ctx context.Context, topicID, subscriptionID string) (*pubsub.Subscription, error)
- func (p *PubSub) Topic(ctx context.Context, topicID string) (*pubsub.Topic, error)
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 (*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
type ListOptions ¶
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 (*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
Click to show internal directories.
Click to hide internal directories.