-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
func NewClient(ctx context.Context, region string) (*s3.Client, error) { | ||
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := s3.NewFromConfig(cfg) | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package s3 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
|
||
awsutil "github.com/devstream-io/devstream/internal/pkg/aws/util" | ||
) | ||
|
||
type S3File struct { | ||
Bucket string | ||
Region string | ||
Key string | ||
ctx context.Context | ||
client *s3.Client | ||
} | ||
|
||
func NewS3File(ctx context.Context, bucket, region, key string) (*S3File, error) { | ||
file := S3File{ | ||
Bucket: bucket, | ||
Region: region, | ||
Key: key, | ||
ctx: ctx, | ||
} | ||
|
||
client, err := NewClient(file.ctx, file.Region) | ||
if err != nil { | ||
return nil, err | ||
} | ||
file.client = client | ||
|
||
return &file, nil | ||
} | ||
|
||
func (f *S3File) Put(data []byte) error { | ||
params := &s3.PutObjectInput{ | ||
Bucket: aws.String(f.Bucket), | ||
Key: aws.String(f.Key), | ||
Body: bytes.NewReader(data), | ||
} | ||
_, err := f.client.PutObject(f.ctx, params) | ||
if err != nil { | ||
awsutil.LogAWSError(err) | ||
return fmt.Errorf("failed to upload %s to bucket %s", f.Key, f.Bucket) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *S3File) Get() ([]byte, error) { | ||
params := &s3.GetObjectInput{ | ||
Bucket: aws.String(f.Bucket), | ||
Key: aws.String(f.Key), | ||
} | ||
out, err := f.client.GetObject(f.ctx, params) | ||
if err != nil { | ||
awsutil.LogAWSError(err) | ||
return nil, fmt.Errorf("failed to download %s from bucket %s", f.Key, f.Bucket) | ||
} | ||
|
||
defer out.Body.Close() | ||
data, err := ioutil.ReadAll(out.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package util | ||
|
||
import "github.com/devstream-io/devstream/pkg/util/log" | ||
|
||
func LogAWSError(err error) { | ||
if err == nil { | ||
return | ||
} | ||
log.Errorf("AWS error: %s", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/devstream-io/devstream/internal/pkg/aws/s3" | ||
"github.com/devstream-io/devstream/internal/pkg/backend" | ||
"github.com/devstream-io/devstream/pkg/util/log" | ||
) | ||
|
||
var _ backend.Backend = (*S3Backend)(nil) | ||
|
||
const DefaultKey = "devstream.state" | ||
|
||
type S3Backend struct { | ||
file *s3.S3File | ||
} | ||
|
||
func NewS3Backend(bucket, region, key string) *S3Backend { | ||
file, err := s3.NewS3File(context.Background(), bucket, region, key) | ||
if err != nil { | ||
log.Fatalf("Creating remote state file %s failed.", key) | ||
} | ||
return &S3Backend{ | ||
file: file, | ||
} | ||
} | ||
|
||
func (b *S3Backend) Read() ([]byte, error) { | ||
return b.file.Get() | ||
} | ||
|
||
func (b *S3Backend) Write(data []byte) error { | ||
return b.file.Put(data) | ||
} |