|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 10 | + "github.com/aws/aws-sdk-go/aws/session" |
| 11 | + "github.com/aws/aws-sdk-go/service/s3" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +type S3Client struct { |
| 16 | + cfg *viper.Viper |
| 17 | + *s3.S3 |
| 18 | +} |
| 19 | + |
| 20 | +// BucketConfigKey is a type for bucket config keys |
| 21 | +// that are used to get bucket config from viper |
| 22 | +// config file. This is not the bucket name itself. |
| 23 | +type BucketConfigKey string |
| 24 | + |
| 25 | +const ( |
| 26 | + // UsersBucketConfigKey is a bucket config key for users bucket |
| 27 | + // This is not the bucket name itself. |
| 28 | + UsersBucketConfigKey BucketConfigKey = "users" |
| 29 | +) |
| 30 | + |
| 31 | +// NewS3Client creates a new S3 client with the given bucket config key. |
| 32 | +func NewS3Client(bucketConfigKey BucketConfigKey) (*S3Client, error) { |
| 33 | + cfg := viper.Sub("api.s3." + string(UsersBucketConfigKey)) |
| 34 | + s, err := session.NewSession(aws.NewConfig(). |
| 35 | + WithEndpoint(cfg.GetString("endpoint")). |
| 36 | + WithRegion(cfg.GetString("region")). |
| 37 | + WithCredentials(credentials.NewCredentials(&credentials.EnvProvider{})), |
| 38 | + ) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + return &S3Client{ |
| 44 | + cfg: cfg, |
| 45 | + S3: s3.New( |
| 46 | + s, |
| 47 | + aws.NewConfig(). |
| 48 | + WithS3ForcePathStyle(cfg.GetBool("forcePathStyle")), |
| 49 | + ), |
| 50 | + }, nil |
| 51 | +} |
| 52 | + |
| 53 | +// PresignedUploadURL returns a presigned upload URL for the given key. |
| 54 | +// The URL will expire after the given expiration duration. The URL will |
| 55 | +// be valid for the given content type and content length and will be |
| 56 | +// uploaded with the given ACL. |
| 57 | +func (c *S3Client) PresignedUploadURL(key, contentType string, contentLength int64, acl string, expiration time.Duration) (string, error) { |
| 58 | + request, _ := c.PutObjectRequest(&s3.PutObjectInput{ |
| 59 | + Bucket: aws.String(c.cfg.GetString("bucket")), |
| 60 | + Key: aws.String(key), |
| 61 | + ContentType: aws.String(contentType), |
| 62 | + ContentLength: aws.Int64(contentLength), |
| 63 | + ACL: aws.String(acl), |
| 64 | + }) |
| 65 | + |
| 66 | + return request.Presign(expiration) |
| 67 | +} |
| 68 | + |
| 69 | +// GetBucket returns the bucket name from the config. |
| 70 | +func (c *S3Client) GetBucket() string { |
| 71 | + return c.cfg.GetString("bucket") |
| 72 | +} |
| 73 | + |
| 74 | +// GetBaseURL returns the base URL for the bucket. This is used to |
| 75 | +// construct the full URL of the bucket based if the bucket is not |
| 76 | +// configured to use path style URLs. If the bucket is configured to |
| 77 | +// use path style URLs, the base URL will be the same as the endpoint. |
| 78 | +func (c *S3Client) GetBaseURL() string { |
| 79 | + s3Endpoint := c.cfg.GetString("endpoint") |
| 80 | + if !c.cfg.GetBool("forcePathStyle") { |
| 81 | + s3Endpoint = regexp.MustCompile(`://`).ReplaceAllString( |
| 82 | + s3Endpoint, |
| 83 | + fmt.Sprintf("://%s.", c.cfg.GetString("bucket")), |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + return s3Endpoint |
| 88 | +} |
0 commit comments