gos3

package module
v0.0.2-beta Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: MIT Imports: 10 Imported by: 0

README

gos3 Build Status Go Report Card License

Using S3 made easier. Use S3 as you use your local file system with this simple wrapper over github.com/aws/aws-sdk-go.

Includes

  • Uploading a file
  • Downloading a file
  • Deleting a file
  • Getting a pre-signed link to the file

Example

package main

import (
    "github.com/IamFaizanKhalid/gos3"
    "io/ioutil"
    "log"
    "os"
    "time"
)

func main() {
    // Getting client
    client, err := gos3.NewClient(&gos3.Config{
        Endpoint:        "http://localhost:4566",
        Region:          "us-east-1",
        AccessKeyId:     "test",
        SecretAccessKey: "test",
    })
    if err != nil {
        log.Panicln(err)
    }

    // Select a bucket    
    bucket, err := client.SelectBucket("test-bucket")
    if err != nil {
        log.Panicln(err)
    }

    // Upload a file
    file, err := os.Open("README.md")
    if err != nil {
        log.Panicln(err)
    }

    err = bucket.Upload(file, file.Name(), "backup")
    if err != nil {
        log.Panicln(err)
    }

    // Download a file
    b, _, err := bucket.Download("backup/README.md")
    if err != nil {
        log.Panicln(err)
    }

    err = ioutil.WriteFile("README.md", b, os.ModePerm)
    if err != nil {
        log.Panicln(err)
    }

    // Get pre-signed link to the file
    link, err := bucket.GetPreSignedLink("backup/README.md", time.Hour)
    if err != nil {
        log.Panicln(err)
    }

    log.Printf("Pre-signed link: %s\n", link)

    // Delete the file
    err = bucket.Delete("backup/README.md")
    if err != nil {
        log.Panicln(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket interface {
	// Upload a file to selected bucket
	Upload(file multipart.File, fileName string, destination string) error

	// List files in the given directory of the selected bucket
	List(directory string) ([]File, error)

	// Download file from the selected bucket
	Download(filePath string) ([]byte, int64, error)

	// Delete file from the selected bucket
	Delete(filePath string) error

	// GetPreSignedLink of a file from the selected bucket with an expiry time
	GetPreSignedLink(filePath string, expiry time.Duration) (string, error)
}

Bucket represents a selected bucket from a client

type Client

type Client interface {
	// CheckConnection returns nil when s3 can be accessible on the given endpoint,
	// or error otherwise
	CheckConnection() error

	// SelectBucket returns an object with a bucket selected.
	// It returns an error if connection to s3 fails.
	SelectBucket(bucketName string) (Bucket, error)
}

Client represents a gos3 client

func NewClient

func NewClient(config *Config) (Client, error)

NewClient returns a Client to use s3

type Config

type Config struct {
	Endpoint        string
	Region          string
	AccessKeyId     string
	SecretAccessKey string
}

Config used to connect to a s3 bucket

type File

type File struct {
	Name         string    `json:"name"`
	Size         int64     `json:"size"`
	LastModified time.Time `json:"last_modified"`
}

File contains file information returned from List()

Jump to

Keyboard shortcuts

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