Skip to content
Snippets Groups Projects
Select Git revision
  • ac44bce0012586db11242f5633728162e865ec71
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

s3.go

Blame
  • s3.go 7.16 KiB
    package s3
    
    import (
    	"bytes"
    	"encoding/binary"
    	"fmt"
    	"net/url"
    	"os"
    	"path"
    	"strings"
    	"time"
    
    	"github.com/aws/aws-sdk-go/aws"
    	"github.com/aws/aws-sdk-go/service/s3"
    	"github.com/google/uuid"
    )
    
    // S3UploadResponse defines the structure of a standard JSON response to a PDF/CSV/etc request.
    type S3UploadResponse struct {
    	URL      string `json:"url"`
    	Filename string `json:"filename"`
    	Bucket   string `json:"bucket"`
    	FileSize int    `json:"file_size"`
    }
    
    type MIMEType string
    
    const (
    	// MIMETypePDF defines the constant for the PDF MIME type.
    	MIMETypePDF MIMEType = "application/pdf"
    
    	// MIMETypeCSV defines the constant for the CSV MIME type.
    	MIMETypeCSV MIMEType = "text/csv"
    
    	// MIMETypeZIP defines the constant for the ZIP MIME type.
    	MIMETypeZIP MIMEType = "application/zip"
    
    	// MIMETypeJSON defines the constant for the JSON MIME type.
    	MIMETypeJSON MIMEType = "application/json"
    
    	// MIMETypeText defines the constant for the Plain text MIME type.
    	MIMETypeText MIMEType = "text/plain"
    
    	// MIMETypeImage defines the constant for the Image MIME type.
    	MIMETypeImage MIMEType = "image/*"
    
    	// MIMETypeDefault defines the constant for the default MIME type.
    	MIMETypeDefault MIMEType = "application/octet-stream"
    )
    
    type SessionWithHelpers struct {
    	S3Session *s3.S3
    }
    
    func NewSession(session *s3.S3) *SessionWithHelpers {
    	return &SessionWithHelpers{
    		S3Session: session,
    	}
    }
    
    func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadata *map[string]*string, isDebug bool) (*s3.PutObjectOutput, error) {
    	mimeType := getTypeForFilename(fileName)
    	putInput := &s3.PutObjectInput{
    		Bucket:      aws.String(bucket),
    		Key:         aws.String(fileName),
    		ContentType: aws.String(string(mimeType)),
    		Body:        bytes.NewReader(data),
    	}
    
    	if metadata != nil {