Skip to content

Instantly share code, notes, and snippets.

@hernanhrm
Created July 16, 2022 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernanhrm/a36e4715c68b381eaf2f0cd16f362ee1 to your computer and use it in GitHub Desktop.
Save hernanhrm/a36e4715c68b381eaf2f0cd16f362ee1 to your computer and use it in GitHub Desktop.
// 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