Created
July 16, 2022 02:09
-
-
Save hernanhrm/a36e4715c68b381eaf2f0cd16f362ee1 to your computer and use it in GitHub Desktop.
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
// Presign signs a key object with an expiry time | |
func (s Service) Presign(key string) (string, error) { | |
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s.s3.amazonaws.com/%s", s.Bucket, key), nil) | |
if err != nil { | |
return "", fmt.Errorf("s3.Presign.http.NewRequest(): %w", err) | |
} | |
// we sign the request with the signTime and a time expiration of 1 day | |
_, err = s.Signer.Presign(req, nil, "s3", *s.Session.Config.Region, time.Hour*24, getSignTime()) | |
if err != nil { | |
return "", fmt.Errorf("s3.Presign.v4.NewSigner(): %w", err) | |
} | |
return req.URL.String(), nil | |
} | |
// getSignTime returns a truncated time to day precision | |
// with this precision, will get the same signature during the day | |
func getSignTime() time.Time { | |
return time.Now().Truncate(time.Hour * 24).UTC() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment