Categorygithub.com/nguyengg/go-aws-commons/s3writer
modulepackage
0.1.1
Repository: https://github.com/nguyengg/go-aws-commons.git
Documentation: pkg.go.dev

# README

Implements io.Writer and io.ReaderFrom to upload to S3

Go Reference

This module provides implementations of io.Writer and io.ReaderFrom for S3 uploading needs.

Get with:

go get github.com/nguyengg/go-aws-commons/s3writer
package main

import (
	"context"
	"log"
	"os"
	"os/signal"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
	"github.com/nguyengg/go-aws-commons/s3writer"
)

func main() {
	ctx, stop := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
	defer stop()

	cfg, err := config.LoadDefaultConfig(ctx)
	if err != nil {
		log.Fatal(err)
	}

	client := s3.NewFromConfig(cfg)

	// open the file to get io.Reader and size.
	f, _ := os.Open("/path/to/file")
	fi, _ := f.Stat()

	// s3writer.Writer implements io.Writer and io.ReaderFrom so I can start piping local file to upload.
	// if running as CLI, s3writer.WithProgressBar will show a progress bar displaying progress.
	// otherwise, use s3writer.WithProgressLogger or s3writer.WithProgressLoggerAndSize instead.
	w, err := s3writer.New(ctx, client, &s3.PutObjectInput{
		Bucket: aws.String("my-bucket"),
		Key:    aws.String("my-key"),
	}, s3writer.WithProgressBar(fi.Size()))
	if err != nil {
		log.Fatal(err)
	}

	// either way below will work.
	_, err = f.WriteTo(w)
	//_, err = w.ReadFrom(f)
	_ = f.Close()
}

# Functions

New returns a Writer given the PutObject input parameters.
WithProgressBar adds a progress bar that displays download progress.
WithProgressLogger adds a progress logger that logs upload progress with the given interval.
WithProgressLoggerAndSize is variant of WithProgressLogger with an expected number of bytes.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
DefaultConcurrency is the default value for Options.Concurrency.
MaxObjectSize is the maximum size of an S3 object.
MaxPartCount is the maximum number of parts per upload.
MaxPartSize is the maximum number of bytes per part upload.
MinPartSize is the minimum number of bytes per part upload.

# Variables

ErrClosed is returned by all Writer write methods after Close returns.

# Structs

MultipartUploadError is the error returns from any writes including Writer.Close if there was an error while using multipart upload.
Options customises the returned Writer of NewWriter.

# Interfaces

Writer uses either a single PutObject or multipart upload to upload content to S3.
WriterClient abstracts the S3 APIs that are needed to implement Writer.

# Type aliases

No description provided by the author