diff options
Diffstat (limited to 'internal/storage/storage.go')
-rw-r--r-- | internal/storage/storage.go | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 6541a1fc5..f73185268 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -41,6 +41,13 @@ const ( urlCacheExpiryFrequency = time.Minute * 5 ) +// PresignedURL represents a pre signed S3 URL with +// an expiry time. +type PresignedURL struct { + *url.URL + Expiry time.Time // link expires at this time +} + // ErrAlreadyExists is a ptr to underlying storage.ErrAlreadyExists, // to put the related errors in the same package as our storage wrapper. var ErrAlreadyExists = storage.ErrAlreadyExists @@ -54,11 +61,11 @@ type Driver struct { // S3-only parameters Proxy bool Bucket string - PresignedCache *ttl.Cache[string, *url.URL] + PresignedCache *ttl.Cache[string, PresignedURL] } // URL will return a presigned GET object URL, but only if running on S3 storage with proxying disabled. -func (d *Driver) URL(ctx context.Context, key string) *url.URL { +func (d *Driver) URL(ctx context.Context, key string) *PresignedURL { // Check whether S3 *without* proxying is enabled s3, ok := d.Storage.(*storage.S3Storage) if !ok || d.Proxy { @@ -72,7 +79,7 @@ func (d *Driver) URL(ctx context.Context, key string) *url.URL { d.PresignedCache.Unlock() if ok { - return e.Value + return &e.Value } u, err := s3.Client().PresignedGetObject(ctx, d.Bucket, key, urlCacheTTL, url.Values{ @@ -82,8 +89,14 @@ func (d *Driver) URL(ctx context.Context, key string) *url.URL { // If URL request fails, fallback is to fetch the file. So ignore the error here return nil } - d.PresignedCache.Set(key, u) - return u + + psu := PresignedURL{ + URL: u, + Expiry: time.Now().Add(urlCacheTTL), // link expires in 24h time + } + + d.PresignedCache.Set(key, psu) + return &psu } func AutoConfig() (*Driver, error) { @@ -151,7 +164,7 @@ func NewS3Storage() (*Driver, error) { } // ttl should be lower than the expiry used by S3 to avoid serving invalid URLs - presignedCache := ttl.New[string, *url.URL](0, 1000, urlCacheTTL-urlCacheExpiryFrequency) + presignedCache := ttl.New[string, PresignedURL](0, 1000, urlCacheTTL-urlCacheExpiryFrequency) presignedCache.Start(urlCacheExpiryFrequency) return &Driver{ |