pkg

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

pkg

This directory contains the source code for r2's main functionality. Functions defined here are to be used by the CLI and are publicly exported for API use.

Architecture

  • client.go contains all R2 client-level operations (e.g. configuration, bucket creation, etc.)
  • bucket.go contains all bucket-level operations (e.g. listing objects, fetching objects, etc.)
  • helpers.go contains miscellaneous helper functions used throughout the CLI

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(slice []string, str string) bool

Contains checks if a string is in a slice of strings.

func IsR2URI

func IsR2URI(uri string) bool

IsR2URI checks if a string is an R2 URI. R2 URI's start with r2://

func RemoveR2URIPrefix

func RemoveR2URIPrefix(uri string) string

RemoveR2URIPrefix removes the r2:// prefix from an R2 URI.

Types

type Config

type Config struct {
	Profile         string
	AccountID       string
	AccessKeyID     string
	SecretAccessKey string
}

Config holds the configuration for the R2 client. This is used to authenticate and connect to the R2 API. The profile is the name of the profile in the ~/.r2 configuration file. The account ID is the ID of the R2 account. The access key ID and secret access key are the credentials for the account.

type R2Bucket

type R2Bucket struct {
	Client *R2Client
	Name   string
}

R2Bucket represents a Cloudflare R2 bucket, storing the bucket's name and R2 client used to access the bucket.

func (*R2Bucket) Copy

func (b *R2Bucket) Copy(bucketPath string, copyToURI R2URI)

Copy copies an object from a bucket to another bucket. The bucketPath argument takes the path to the object in the bucket. The copyToURI argument takes the URI of the bucket to copy the object to. This method is a wrapper around the S3 CopyObject API call.

func (*R2Bucket) Delete

func (b *R2Bucket) Delete(bucketPath string)

Delete deletes an object from a bucket. The bucketPath argument takes the path to the object in the bucket. This method is a wrapper around the S3 DeleteObject API call.

func (*R2Bucket) Download

func (b *R2Bucket) Download(bucketPath, localPath string)

Download downloads an object from a bucket to a local file. The bucketPath argument takes the path to the object in the bucket. The localPath argument takes the path to the local file to download to. This method is a wrapper around Get, which returns an io.ReadCloser.

func (*R2Bucket) Get

func (b *R2Bucket) Get(bucketPath string) io.ReadCloser

Get gets an object from a bucket. The bucketPath argument takes the path to the object in the bucket. This method returns an io.ReadCloser, which can be used to read the object's contents. This method is a wrapper around the S3 GetObject API call.

func (*R2Bucket) GetObjectPaths

func (b *R2Bucket) GetObjectPaths() []string

GetObjectPaths returns a list of all object paths in a bucket, represented as strings. This method is a wrapper around GetObjects, which returns a list of types.Object structs.

func (*R2Bucket) GetObjects

func (b *R2Bucket) GetObjects() []types.Object

GetObjects returns a list of all objects in a bucket. This method leverages S3's ListObjectsV2 API call. The returned list of objects is of type types.Object, which is a struct containing all available information about the object, such as its name, size, and last modified date.

func (*R2Bucket) PrintObjects

func (b *R2Bucket) PrintObjects()

PrintObjects prints a list of all objects in a bucket. This method is a wrapper around GetObjects, which returns a list of types.Object structs. The returned list of objects is formatted as a table with the following columns: last modified date, file size, file name. The file size column is formatted as a string with the file size and its unit (e.g. 1.2 MB).

func (*R2Bucket) Put

func (b *R2Bucket) Put(file io.Reader, bucketPath string) error

Put puts an object into a bucket. The inputted object is represented as an io.Reader, which can be created from a file, a string, or any other type that implements the io.Reader interface. The bucketPath argument takes the path for the object to be put in the bucket.

func (*R2Bucket) SyncLocalToR2

func (b *R2Bucket) SyncLocalToR2(sourcePath string)

SyncLocalToR2 syncs a local directory to an R2 bucket. The sourcePath argument takes the path to the local directory to sync. This method iterates through the local directory and uploads any new or changed files to the bucket.

func (*R2Bucket) SyncR2ToLocal

func (b *R2Bucket) SyncR2ToLocal(destinationPath string)

SyncR2ToLocal syncs an R2 bucket to a local directory. The destinationPath argument takes the path to the local directory to sync. This method iterates through the bucket and downloads any new or changed files to the local directory.

func (*R2Bucket) SyncR2ToR2

func (b *R2Bucket) SyncR2ToR2(destBucket R2Bucket)

SyncR2ToR2 syncs an R2 bucket to another R2 bucket. The destBucket argument takes the bucket to sync to. This method iterates through the bucket and copies any new or changed files to the destination bucket.

func (*R2Bucket) Upload

func (b *R2Bucket) Upload(localPath, bucketPath string)

Upload uploads a local file to a bucket. The localPath argument takes the path to the local file to be uploaded. The bucketPath argument takes the path for the object to be put in the bucket. This method is a wrapper around Put, which takes an io.Reader as an argument.

type R2Client

type R2Client struct {
	s3.Client
}

R2Client is a wrapper around the S3 client that provides methods for interacting with R2. This allows us to add methods to the client. The S3 client is embedded in the R2Client struct so that we can use the existing methods of the S3 client without having to re-implement them.

func Client

func Client(c Config) R2Client

Client returns a new R2 client struct so we can add methods to it. The client is configured with the R2 endpoint and credentials for the given profile.

func (*R2Client) Bucket

func (c *R2Client) Bucket(bucketName string) R2Bucket

Bucket receives an R2 client and takes a bucket name as an argument, returning a configured R2Bucket struct. This allows for simple bucket-level operations. For example, you can create a bucket struct as so:

client := Client(Config{...})
bucket := client.Bucket("my-bucket")

// Then, you can perform bucket-level operations easily
bucket.Put("my-local-file.txt", "my-remote-file.txt")

func (*R2Client) MakeBucket

func (c *R2Client) MakeBucket(name string)

MakeBucket creates a new R2 bucket with the given name. The bucket is created in the account associated with the R2 client. The bucket name must be unique across all existing bucket names in the account.

func (*R2Client) PrintBuckets

func (c *R2Client) PrintBuckets()

PrintBuckets prints the creation date and name of each bucket in the R2 account.

func (*R2Client) RemoveBucket

func (c *R2Client) RemoveBucket(bucket string)

RemoveBucket removes the bucket with the given name from the R2 account. The bucket must be empty before it can be removed.

type R2PresignClient

type R2PresignClient struct {
	s3.PresignClient
}

R2PresignClient is a wrapper around the S3 presign client that provides methods for interacting with R2. This allows us to add methods to the client. The S3 presign client is embedded in the R2PresignClient struct so that we can use the existing methods of the S3 presign client without having to re-implement them.

func PresignClient

func PresignClient(c Config) R2PresignClient

PresignClient returns a new R2 presign client struct so we can add methods to it. The client is configured with the R2 endpoint and credentials for the given profile. The presign client is used for generating presigned URLs.

func (*R2PresignClient) GetURL

func (pc *R2PresignClient) GetURL(uri R2URI) string

GetURL returns a presigned URL for an object to get from a bucket. The uri argument takes the URI of the object in the bucket. This method is a wrapper around the S3 PresignGetObject API call.

func (*R2PresignClient) PutURL

func (pc *R2PresignClient) PutURL(uri R2URI) string

PutURL returns a presigned URL for an object to put in a bucket. The uri argument takes the URI of the object in the bucket. This method is a wrapper around the S3 PresignPutObject API call.

type R2URI

type R2URI struct {
	Bucket string
	Path   string
}

R2URI represents an R2 URI. It contains the bucket name and the path to the file.

func ParseR2URI

func ParseR2URI(uri string) R2URI

ParseR2URI parses an R2 URI and returns a R2URI struct. It assumes that the URI is valid and does not check if the bucket or file exists.

Jump to

Keyboard shortcuts

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