s3_service

package module
v0.0.0-...-416b7c3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2021 License: MIT Imports: 8 Imported by: 0

README

s3-service

Service client for AWS S3 using Wire for dependency injection

Example: Save a file
package main

import (
	"fmt"
	s3 "github.com/marthinal/s3-service"
	"log"
	"net/http"
)

const MaxUploadSize = 1024 * 1024

func main()  {
	fs := s3.GetFS()
	s := &Service{fs: fs}
	err := http.ListenAndServe("0.0.0.0:6090", s)
	if err != nil {
		log.Fatal("Error starting the service")
	}
}

type Service struct {
	fs s3.FS
}

func (s Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fs := s.fs
	switch r.URL.Path {
	default:
		http.Error(w, "not found", http.StatusNotFound)
		return
	case "/upload":
		if r.Method != "POST" {
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
		w.Header().Set("Access-Control-Allow-Origin", "*")
		err := r.ParseMultipartForm(100000)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		for _, h := range r.MultipartForm.File["images"] {
			if h.Size > MaxUploadSize {
				http.Error(w, fmt.Sprintf("The uploaded image is too big: %s", h.Filename), http.StatusBadRequest)
				return
			}
			file, err := h.Open()
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			defer file.Close()

			err = fs.Save("EXAMPLE_FILENAME.png", file)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			return
		}
	}
}

This is WIP. I just added the function to upload files.

You need these env variables:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_REGION
  • AWS_BUCKET_NAME

AWS SDK gets the values by default.

See https://github.com/google/wire for more info about Wire.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

type FS struct {
	// contains filtered or unexported fields
}

func GetFS

func GetFS() FS

func InitializeFS

func InitializeFS() FS

func (FS) Save

func (fs FS) Save(keyName string, file io.ReadSeeker) error

type FileSystem

type FileSystem interface {
	Save(keyName string, file io.ReadSeeker) error
}

Jump to

Keyboard shortcuts

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