github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/ingestion/uploader/s3_uploader.go (about)

     1  package uploader
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  
     7  	"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
     8  	"github.com/aws/aws-sdk-go-v2/service/s3"
     9  	"github.com/rs/zerolog"
    10  
    11  	"github.com/onflow/flow-go/engine/execution"
    12  )
    13  
    14  var _ Uploader = (*S3Uploader)(nil)
    15  
    16  // S3Uploader is a S3 implementation of the uploader interface.
    17  type S3Uploader struct {
    18  	ctx    context.Context
    19  	log    zerolog.Logger
    20  	client *s3.Client
    21  	bucket string
    22  }
    23  
    24  // NewS3Uploader returns a new S3 uploader instance.
    25  func NewS3Uploader(ctx context.Context, client *s3.Client, bucket string, log zerolog.Logger) *S3Uploader {
    26  	return &S3Uploader{
    27  		ctx:    ctx,
    28  		log:    log,
    29  		client: client,
    30  		bucket: bucket,
    31  	}
    32  }
    33  
    34  // Upload uploads the given computation result to the configured S3 bucket.
    35  func (u *S3Uploader) Upload(result *execution.ComputationResult) error {
    36  	uploader := manager.NewUploader(u.client)
    37  	key := GCPBlockDataObjectName(result)
    38  	buf := &bytes.Buffer{}
    39  	err := WriteComputationResultsTo(result, buf)
    40  
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	_, err = uploader.Upload(u.ctx, &s3.PutObjectInput{
    46  		Bucket: &u.bucket,
    47  		Key:    &key,
    48  		Body:   buf,
    49  	})
    50  
    51  	return err
    52  }