github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/s3.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package artifacts
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/minio/minio-go/v7"
    25  	"github.com/minio/minio-go/v7/pkg/credentials"
    26  
    27  	"github.com/argoproj/argo-events/common/logging"
    28  	apicommon "github.com/argoproj/argo-events/pkg/apis/common"
    29  )
    30  
    31  // S3Reader implements the ArtifactReader interface and allows reading artifacts from S3 compatible API store
    32  type S3Reader struct {
    33  	client *minio.Client
    34  	s3     *apicommon.S3Artifact
    35  	creds  *Credentials
    36  }
    37  
    38  // NewS3Reader creates a new ArtifactReader for an S3 compatible store
    39  func NewS3Reader(s3 *apicommon.S3Artifact, creds *Credentials) (ArtifactReader, error) {
    40  	client, err := NewMinioClient(s3, *creds)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return &S3Reader{
    45  		client: client,
    46  		s3:     s3,
    47  		creds:  creds,
    48  	}, nil
    49  }
    50  
    51  func (reader *S3Reader) Read() ([]byte, error) {
    52  	log := logging.NewArgoEventsLogger()
    53  	log.Debugf("reading s3Artifact from %s/%s", reader.s3.Bucket.Name, reader.s3.Bucket.Key)
    54  	obj, err := reader.client.GetObject(context.Background(), reader.s3.Bucket.Name, reader.s3.Bucket.Key, minio.GetObjectOptions{})
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	defer func() {
    59  		if err := obj.Close(); err != nil {
    60  			fmt.Printf("failed to close object. err: %+v", err)
    61  		}
    62  	}()
    63  
    64  	b, err := io.ReadAll(io.LimitReader(obj, 1024*1224))
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return b, nil
    69  }
    70  
    71  // NewMinioClient instantiates a new minio client object to access s3 compatible APIs
    72  func NewMinioClient(s3 *apicommon.S3Artifact, creds Credentials) (*minio.Client, error) {
    73  	var minioClient *minio.Client
    74  	var err error
    75  	if s3.Region != "" {
    76  		minioClient, err = minio.New(s3.Endpoint, &minio.Options{
    77  			Creds: credentials.NewStaticV4(creds.accessKey, creds.secretKey, ""), Secure: !s3.Insecure, Region: s3.Region})
    78  	} else {
    79  		minioClient, err = minio.New(s3.Endpoint, &minio.Options{
    80  			Creds: credentials.NewStaticV4(creds.accessKey, creds.secretKey, ""), Secure: !s3.Insecure})
    81  	}
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	return minioClient, nil
    86  }