-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from sboy99/feat/cloud
feat: backup directly to the cloud
- Loading branch information
Showing
13 changed files
with
386 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,6 @@ generated/ | |
# Config | ||
config.json | ||
config.yml | ||
|
||
# Secrets | ||
.env |
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
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
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,91 @@ | ||
package storage | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/sboy99/go-vault/config" | ||
"github.com/sboy99/go-vault/pkg/utils" | ||
) | ||
|
||
type AWSCloudStorage struct { | ||
Region string | ||
BucketName string | ||
AccessKeyId string | ||
AccessKeySecret string | ||
Endpoint string | ||
Token string | ||
} | ||
|
||
func NewAWSCloudStorage() *AWSCloudStorage { | ||
cfg := config.GetConfig() | ||
return &AWSCloudStorage{ | ||
Region: cfg.Storage.Cloud.AWS.Region, | ||
BucketName: cfg.Storage.Cloud.AWS.BucketName, | ||
AccessKeyId: cfg.Storage.Cloud.AWS.AccessKeyId, | ||
AccessKeySecret: cfg.Storage.Cloud.AWS.AccessKeySecret, | ||
Endpoint: cfg.Storage.Cloud.AWS.Endpoint, | ||
Token: "", // TODO: Add token but not required for now | ||
} | ||
} | ||
|
||
func (a *AWSCloudStorage) Upload(filename string, data []byte) error { | ||
s3, err := a.getS3() | ||
if err != nil { | ||
return err | ||
} | ||
key := a.buildKey(filename) | ||
s3Object := a.createPutObjectInput(key, data) | ||
if _, err = s3.PutObject(s3Object); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (a *AWSCloudStorage) Download(filename string) ([]byte, error) { | ||
return nil, fmt.Errorf("Not Impl") | ||
} | ||
|
||
func (a *AWSCloudStorage) Delete(filename string) error { | ||
return fmt.Errorf("Not Impl") | ||
} | ||
|
||
func (a *AWSCloudStorage) getS3() (*s3.S3, error) { | ||
sess, err := a.getSession() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s3.New(sess), nil | ||
} | ||
|
||
func (a *AWSCloudStorage) getSession() (*session.Session, error) { | ||
creds := credentials.NewStaticCredentials(a.AccessKeyId, a.AccessKeySecret, a.Token) | ||
if a.Endpoint == "default" { | ||
return session.NewSession(&aws.Config{ | ||
Region: aws.String(a.Region), | ||
Credentials: creds, | ||
}) | ||
} | ||
return session.NewSession(&aws.Config{ | ||
Region: aws.String(a.Region), | ||
Endpoint: aws.String(a.Endpoint), | ||
S3ForcePathStyle: aws.Bool(true), | ||
Credentials: creds, | ||
}) | ||
} | ||
|
||
func (a *AWSCloudStorage) createPutObjectInput(key string, data []byte) *s3.PutObjectInput { | ||
return &s3.PutObjectInput{ | ||
Key: aws.String(key), | ||
Bucket: aws.String(a.BucketName), | ||
Body: bytes.NewReader(data), | ||
} | ||
} | ||
|
||
func (a *AWSCloudStorage) buildKey(filename string) string { | ||
return utils.GetUnixTimeStamp() + "_" + filename | ||
} |
Oops, something went wrong.