Methods
Read
Read a file from the bucket.
func (c *Bucket) Read(key string) ([]byte, error)
Parameters
The key/path of the file to read
Returns
The file contents as a byte slice
Error if the operation fails
Example:
content, err := client.Image.Read("path/to/file.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
Write
Write a file to the bucket.
func (c *Bucket) Write(key string, data []byte) error
Parameters
The key/path where the file should be stored
The file contents as a byte slice
Returns
Error if the operation fails
Example:
err := client.Image.Write("path/to/file.txt", []byte("Hello, World!"))
if err != nil {
log.Fatal(err)
}
Delete
Delete a file from the bucket.
func (c *Bucket) Delete(key string) error
Parameters
The key/path of the file to delete
Returns
Error if the operation fails
Example:
err := client.Image.Delete("path/to/file.txt")
if err != nil {
log.Fatal(err)
}
List
List files in the bucket with a given prefix.
func (c *Bucket) List(prefix string) ([]string, error)
Parameters
Filter results to keys starting with this prefix
Returns
Slice of file keys matching the prefix
Error if the operation fails
Example:
// List all files
files, err := client.Image.List("")
if err != nil {
log.Fatal(err)
}
// List files in a specific directory
docs, err := client.Image.List("documents/")
if err != nil {
log.Fatal(err)
}
for _, file := range docs {
fmt.Println(file)
}
Exists
Check if a file exists in the bucket.
func (c *Bucket) Exists(key string) (bool, error)
Parameters
The key/path of the file to check
Returns
True if the file exists, false otherwise
Error if the operation fails
Example:
exists, err := client.Image.Exists("file.txt")
if err != nil {
log.Fatal(err)
}
if exists {
content, _ := client.Image.Read("file.txt")
// Process content
}
Presigned URLs
Generate presigned URLs for secure file access without exposing credentials.
func (c *Bucket) GetDownloadURL(key string, opts ...PresignUrlOption) (string, error)
func (c *Bucket) GetUploadURL(key string, opts ...PresignUrlOption) (string, error)
Parameters
opts
...PresignUrlOption
default:"WithPresignUrlExpiry(5*time.Minute)"
Optional configuration with WithPresignUrlExpiry(duration)
Returns
The presigned URL for the file
Error if the operation fails
Example:
// Download URL (default 5 minute expiry)
downloadURL, err := client.Image.GetDownloadURL("file.txt")
// Upload URL with custom expiry (1 hour)
uploadURL, err := client.Image.GetUploadURL("file.txt",
suga.WithPresignUrlExpiry(time.Hour))