github.com/puellanivis/breton@v0.2.16/lib/files/s3files/writer.go (about)

     1  package s3files
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"net/url"
     7  
     8  	"github.com/puellanivis/breton/lib/files"
     9  	"github.com/puellanivis/breton/lib/files/wrapper"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/s3"
    13  )
    14  
    15  func (h *handler) Create(ctx context.Context, uri *url.URL) (files.Writer, error) {
    16  	bucket, key, err := getBucketKey("create", uri)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	w := wrapper.NewWriter(ctx, uri, func(b []byte) error {
    22  		cl, err := h.getClient(ctx, bucket)
    23  		if err != nil {
    24  			return files.PathError("sync", uri.String(), err)
    25  		}
    26  
    27  		req := &s3.PutObjectInput{
    28  			Bucket: aws.String(bucket),
    29  			Key:    aws.String(key),
    30  			Body:   bytes.NewReader(b),
    31  		}
    32  
    33  		_, err = cl.PutObjectWithContext(ctx, req)
    34  		if err != nil {
    35  			return files.PathError("sync", uri.String(), normalizeError(err))
    36  		}
    37  
    38  		return nil
    39  	})
    40  
    41  	return w, nil
    42  }