diff --git a/conf.example.yml b/conf.example.yml index 37717c7..6e8657f 100644 --- a/conf.example.yml +++ b/conf.example.yml @@ -279,6 +279,7 @@ destination: token: your-token # can be an environment variable, just don't add a $ in front of it zip: false # if true, will zip the entire git repo into a single zip file and upload that instead usessl: true # wheter to use ssl or not + storageclass: "" # storage class for all repos to be uploaded to this S3 bucket. E.g. for AWS: STANDARD, STANDARD_IA, GLACIER, etc. cron: 0 22 * * * # optional - when cron is not provided, the program runs once and exits. # Otherwise, it runs according to the cron schedule. # See timezone commentary in docker-compose.yml for making sure this container runs diff --git a/gickup_spec.json b/gickup_spec.json index 40c0e03..1729fc0 100644 --- a/gickup_spec.json +++ b/gickup_spec.json @@ -546,6 +546,10 @@ "zip": { "type": "boolean", "description": "If true, zip the entire Git repo into a single zip file and upload that instead" + }, + "storageclass": { + "type": "string", + "description": "Storage class applied to all repos uploaded to this S3 bucket (optional)" } }, "additionalProperties": false diff --git a/main.go b/main.go index def6785..36e696a 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/cooperspencer/gickup/sourcehut" "github.com/go-git/go-git/v5" "github.com/google/go-cmp/cmp" + "github.com/minio/minio-go/v7" "github.com/alecthomas/kong" "github.com/cooperspencer/gickup/bitbucket" @@ -264,7 +265,9 @@ func backup(repos []types.Repo, conf *types.Conf) { continue } } - err = s3.UploadDirToS3(tempdir, d) + err = s3.UploadDirToS3(tempdir, d, &minio.PutObjectOptions{ + StorageClass: d.StorageClass, + }) if err != nil { log.Error().Str("stage", "s3").Str("endpoint", d.Endpoint).Str("bucket", d.Bucket).Msg(err.Error()) } diff --git a/s3/s3.go b/s3/s3.go index 2f63998..085d661 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -17,7 +17,7 @@ var ( ) // UploadDirToS3 uploads the contents of a directory to S3-compatible storage -func UploadDirToS3(directory string, s3repo types.S3Repo) error { +func UploadDirToS3(directory string, s3repo types.S3Repo, options *minio.PutObjectOptions) error { // Initialize minio client object. client, err := minio.New(s3repo.Endpoint, &minio.Options{ Creds: credentials.NewStaticV4(s3repo.AccessKey, s3repo.SecretKey, s3repo.Token), @@ -51,7 +51,11 @@ func UploadDirToS3(directory string, s3repo types.S3Repo) error { // Upload the file to S3-compatible storage objectName := filepath.ToSlash(path[len(directory)+1:]) // Object name in bucket - _, err = client.PutObject(context.Background(), s3repo.Bucket, objectName, file, stat.Size(), minio.PutObjectOptions{}) + if options == nil { + options = &minio.PutObjectOptions{} + } + + _, err = client.PutObject(context.Background(), s3repo.Bucket, objectName, file, stat.Size(), *options) if err != nil { return err } diff --git a/types/types.go b/types/types.go index 1a7285a..4d0ea67 100644 --- a/types/types.go +++ b/types/types.go @@ -535,15 +535,16 @@ func StatRemote(remoteURL, sshURL string, repo GenRepo) bool { } type S3Repo struct { - Bucket string `yaml:"bucket"` - Endpoint string `yaml:"endpoint"` - AccessKey string `yaml:"accesskey"` - SecretKey string `yaml:"secretkey"` - Token string `yaml:"token"` - Region string `yaml:"region"` - UseSSL bool `yaml:"usessl"` - Structured bool `yaml:"structured"` - Zip bool `yaml:"zip"` + Bucket string `yaml:"bucket"` + Endpoint string `yaml:"endpoint"` + AccessKey string `yaml:"accesskey"` + SecretKey string `yaml:"secretkey"` + Token string `yaml:"token"` + Region string `yaml:"region"` + UseSSL bool `yaml:"usessl"` + Structured bool `yaml:"structured"` + Zip bool `yaml:"zip"` + StorageClass string `yaml:"storageclass"` } func (s3 S3Repo) GetKey(accessString string) (string, error) {