github.com/cs3org/reva/v2@v2.27.7/pkg/storage/fs/s3/upload.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package s3
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/aws/aws-sdk-go/aws"
    25  	"github.com/aws/aws-sdk-go/aws/awserr"
    26  	"github.com/aws/aws-sdk-go/service/s3"
    27  	"github.com/aws/aws-sdk-go/service/s3/s3manager"
    28  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    29  	"github.com/cs3org/reva/v2/pkg/appctx"
    30  	"github.com/cs3org/reva/v2/pkg/errtypes"
    31  	"github.com/cs3org/reva/v2/pkg/storage"
    32  	"github.com/pkg/errors"
    33  )
    34  
    35  func (fs *s3FS) Upload(ctx context.Context, req storage.UploadRequest, uff storage.UploadFinishedFunc) (*provider.ResourceInfo, error) {
    36  	log := appctx.GetLogger(ctx)
    37  
    38  	fn, err := fs.resolve(ctx, req.Ref)
    39  	if err != nil {
    40  		return &provider.ResourceInfo{}, errors.Wrap(err, "error resolving ref")
    41  	}
    42  
    43  	upParams := &s3manager.UploadInput{
    44  		Bucket: aws.String(fs.config.Bucket),
    45  		Key:    aws.String(fn),
    46  		Body:   req.Body,
    47  	}
    48  	uploader := s3manager.NewUploaderWithClient(fs.client)
    49  	result, err := uploader.Upload(upParams)
    50  
    51  	if err != nil {
    52  		log.Error().Err(err)
    53  		if aerr, ok := err.(awserr.Error); ok {
    54  			if aerr.Code() == s3.ErrCodeNoSuchBucket {
    55  				return &provider.ResourceInfo{}, errtypes.NotFound(fn)
    56  			}
    57  		}
    58  		return &provider.ResourceInfo{}, errors.Wrap(err, "s3fs: error creating object "+fn)
    59  	}
    60  
    61  	log.Debug().Interface("result", result) // todo cache etag?
    62  
    63  	// return id, etag and mtime
    64  	ri, err := fs.GetMD(ctx, req.Ref, []string{}, []string{"id", "etag", "mtime"})
    65  	if err != nil {
    66  		return &provider.ResourceInfo{}, err
    67  	}
    68  
    69  	return ri, nil
    70  }
    71  
    72  // InitiateUpload returns upload ids corresponding to different protocols it supports
    73  func (fs *s3FS) InitiateUpload(ctx context.Context, ref *provider.Reference, uploadLength int64, metadata map[string]string) (map[string]string, error) {
    74  	return nil, errtypes.NotSupported("op not supported")
    75  }