Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Implement Write for AWS S3 and Google Cloud Storage objects
Browse files Browse the repository at this point in the history
Also use a context for S3 get object requests.

Signed-off-by: Michael Smith <[email protected]>
  • Loading branch information
MikaelSmith committed Dec 17, 2019
1 parent 35fd8b9 commit 75c31ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion plugin/aws/s3Object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"bytes"
"context"
"io/ioutil"
"strconv"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (o *s3Object) Read(ctx context.Context, size int64, offset int64) ([]byte,
Range: awsSDK.String("bytes=" + strconv.FormatInt(offset, 10) + "-" + strconv.FormatInt(offset+size, 10)),
}

resp, err := o.client.GetObject(request)
resp, err := o.client.GetObjectWithContext(ctx, request)
if err != nil {
return nil, err
}
Expand All @@ -88,9 +89,26 @@ func (o *s3Object) Read(ctx context.Context, size int64, offset int64) ([]byte,
}
}()

activity.Record(ctx, "S3 object read response: %+v", *resp)
return ioutil.ReadAll(resp.Body)
}

func (o *s3Object) Write(ctx context.Context, p []byte) error {
request := &s3Client.PutObjectInput{
Bucket: awsSDK.String(o.bucket),
Key: awsSDK.String(o.key),
Body: bytes.NewReader(p),
}

resp, err := o.client.PutObjectWithContext(ctx, request)
if err != nil {
return err
}

activity.Record(ctx, "S3 object write response: %+v", *resp)
return nil
}

func (o *s3Object) Delete(ctx context.Context) (bool, error) {
_, err := o.client.DeleteObjectWithContext(ctx, &s3Client.DeleteObjectInput{
Bucket: aws.String(o.bucket),
Expand Down
11 changes: 11 additions & 0 deletions plugin/gcp/storageObject.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func (s *storageObject) Read(ctx context.Context, size int64, offset int64) ([]b
return ioutil.ReadAll(rdr)
}

func (s *storageObject) Write(ctx context.Context, p []byte) error {
wr := s.ObjectHandle.NewWriter(ctx)
if _, err := wr.Write(p); err != nil {
wr.Close()
return err
}

// When Close fails we can assume the object update failed.
return wr.Close()
}

func (s *storageObject) Delete(ctx context.Context) (bool, error) {
err := s.ObjectHandle.Delete(ctx)
return true, err
Expand Down

0 comments on commit 75c31ea

Please sign in to comment.