Skip to content

Commit

Permalink
s3: storage class for files in result storage bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
f100024 committed Oct 10, 2023
1 parent c11b178 commit 9a8400d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
4 changes: 4 additions & 0 deletions config/awsconfig/awsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package awsconfig

import (
"flag"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -90,6 +91,8 @@ func WithAWS(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
"Upload ACL for S3 Result Storage")
s3ResultStorageExpiration = fs.Duration("s3-result-storage-expiration", 0,
"S3 Result Storage expiration duration e.g. 24h. Default no expiration")
s3FileStorageClass = fs.String("s3-file-storage-class", "STANDARD",
"S3 File Storage Class. Available values: reduced-redunancy, standard-ia, intelligent-tiering, glacier, deep_archive. Default: standard.")

_, _ = cb()
)
Expand Down Expand Up @@ -182,6 +185,7 @@ func WithAWS(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
s3storage.WithACL(*s3ResultStorageACL),
s3storage.WithSafeChars(*s3SafeChars),
s3storage.WithExpiration(*s3ResultStorageExpiration),
s3storage.WithFileStorageClass(*s3FileStorageClass),
),
)
}
Expand Down
24 changes: 23 additions & 1 deletion storage/s3storage/option.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package s3storage

import (
"github.com/aws/aws-sdk-go/service/s3"
"strings"
"time"

"github.com/aws/aws-sdk-go/service/s3"
)

// Option S3Storage option
Expand Down Expand Up @@ -70,3 +71,24 @@ func WithExpiration(exp time.Duration) Option {
}
}
}

func WithFileStorageClass(storageClass string) Option {
return func(h *S3Storage) {
switch storageClass {
case "reduced-redunancy":
h.StorageClass = "REDUCED_REDUNDANCY"
case "standard-ia":
h.StorageClass = "STANDARD_IA"
case "onezone-ia":
h.StorageClass = "ONEZONE_IA"
case "intelligent-tiering":
h.StorageClass = "INTELLIGENT_TIERING"
case "glacier":
h.StorageClass = "GLACIER"
case "deep_archive":
h.StorageClass = "DEEP_ARCHIVE"
default:
h.StorageClass = "STANDARD"
}
}
}
37 changes: 20 additions & 17 deletions storage/s3storage/s3storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package s3storage

import (
"context"
"io"
"net/http"
"path/filepath"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"io"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
)

// S3Storage AWS S3 Storage implements imagor.Storage interface
Expand All @@ -24,11 +25,12 @@ type S3Storage struct {
Downloader *s3manager.Downloader
Bucket string

BaseDir string
PathPrefix string
ACL string
SafeChars string
Expiration time.Duration
BaseDir string
PathPrefix string
ACL string
SafeChars string
StorageClass string
Expiration time.Duration

safeChars imagorpath.SafeChars
}
Expand Down Expand Up @@ -128,12 +130,13 @@ func (s *S3Storage) Put(ctx context.Context, image string, blob *imagor.Blob) er
}()
var metadata map[string]*string
input := &s3manager.UploadInput{
ACL: aws.String(s.ACL),
Body: reader,
Bucket: aws.String(s.Bucket),
ContentType: aws.String(blob.ContentType()),
Metadata: metadata,
Key: aws.String(image),
ACL: aws.String(s.ACL),
Body: reader,
Bucket: aws.String(s.Bucket),
ContentType: aws.String(blob.ContentType()),
Metadata: metadata,
Key: aws.String(image),
StorageClass: aws.String(s.StorageClass),
}
_, err = s.Uploader.UploadWithContext(ctx, input)
return err
Expand Down

0 comments on commit 9a8400d

Please sign in to comment.