s3bucket

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

S3Bucket Package

The s3bucket package is an open-source Go package that provides an abstraction for interacting with an S3 bucket. It simplifies common operations such as downloading files, moving files within the bucket, deleting files, and uploading files.

Installation

To use the s3bucket package in your Go project, you can install it using the go get command:

go get github.com/problem-company-toolkit/s3bucket

Usage

To start using the s3bucket package, you need to import it in your Go code:

import "github.com/problem-company-toolkit/s3bucket"
Creating an S3 Bucket

To create a new Bucket object that represents an S3 bucket, you can use the NewS3 function:

config := s3bucket.AWSConfig{
    Session: session,
    Bucket:  "your-bucket-name",
}

bucket := s3bucket.NewS3(config)

The AWSConfig struct requires an AWS session.Session object and the name of the S3 bucket.

Downloading a File

To download a file from the S3 bucket, you can use the DownloadFile method:

reader, err := bucket.DownloadFile("path/to/file.txt")
if err != nil {
    // Handle error
}

defer reader.Close()

// Read the file content from the reader

The DownloadFile method returns an io.ReadCloser that provides access to the downloaded file content. Make sure to close the reader when you're done reading the file.

Moving a File

To move a file within the S3 bucket, you can use the MoveFile method:

err := bucket.MoveFile("source/file.txt", "destination/file.txt")
if err != nil {
    // Handle error
}

The MoveFile method moves the file from the source path to the target path within the S3 bucket.

Deleting a File

To delete a file from the S3 bucket, you can use the DeleteFile method:

err := bucket.DeleteFile("path/to/file.txt")
if err != nil {
    // Handle error
}

The DeleteFile method deletes the specified file from the S3 bucket.

Uploading a File

To upload a file to the S3 bucket, you can use the UploadFile method:

file, err := os.Open("path/to/local/file.txt")
if err != nil {
    // Handle error
}
defer file.Close()

err = bucket.UploadFile(file, "destination/file.txt")
if err != nil {
    // Handle error
}

The UploadFile method takes an io.ReadSeeker that represents the file content and uploads it to the specified destination within the S3 bucket.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AWSConfig

type AWSConfig struct {
	Session *session.Session
	Bucket  string
}

type Bucket

type Bucket interface {
	// Downloads the file from the specified bucket path to the specified host path.
	DownloadFile(bucketKey string) (io.ReadCloser, error)

	// Move file inside the bucket from source to target destination.
	MoveFile(sourceDest, targetDest string) error

	// Deletes the target file in the bucket.
	DeleteFile(targetFile string) error

	// Uplodates a file to the configured S3 bucket.
	UploadFile(content io.ReadSeeker, targetDest string) error

	// Gets a signed URL that allows public access to a resource. Expires after the provided duration.
	GetSignedUrl(bucketKey string, duration time.Duration) (string, error)
}

Abstraction over interacting with a S3 bucket.

func NewS3

func NewS3(
	config AWSConfig,
) Bucket

Jump to

Keyboard shortcuts

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