Documentation
¶
Index ¶
- func Contains(slice []string, str string) bool
- func IsR2URI(uri string) bool
- func RemoveR2URIPrefix(uri string) string
- type Config
- type R2Bucket
- func (b *R2Bucket) Copy(bucketPath string, copyToURI R2URI)
- func (b *R2Bucket) Delete(bucketPath string)
- func (b *R2Bucket) Download(bucketPath, localPath string)
- func (b *R2Bucket) Get(bucketPath string) io.ReadCloser
- func (b *R2Bucket) GetObjectPaths() []string
- func (b *R2Bucket) GetObjects() []types.Object
- func (b *R2Bucket) PrintObjects()
- func (b *R2Bucket) Put(file io.Reader, bucketPath string) error
- func (b *R2Bucket) SyncLocalToR2(sourcePath string)
- func (b *R2Bucket) SyncR2ToLocal(destinationPath string)
- func (b *R2Bucket) SyncR2ToR2(destBucket R2Bucket)
- func (b *R2Bucket) Upload(localPath, bucketPath string)
- type R2Client
- type R2PresignClient
- type R2URI
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RemoveR2URIPrefix ¶
RemoveR2URIPrefix removes the r2:// prefix from an R2 URI.
Types ¶
type Config ¶
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 ¶
R2Bucket represents a Cloudflare R2 bucket, storing the bucket's name and R2 client used to access the bucket.
func (*R2Bucket) Copy ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type R2Client ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
R2URI represents an R2 URI. It contains the bucket name and the path to the file.
func ParseR2URI ¶
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.