Example #1
0
func generateSignedS3URL(region AWSRegion, bucket string, key string, expiry uint, endpoint string) (string, error) {
	s3ForcePathStyle := false
	if endpoint != "" {
		s3ForcePathStyle = true
	}

	// We want to use the default credentials chain so that it will attempt Env & Instance role creds
	svc := s3.New(&aws.Config{
		Region:           string(region),
		Endpoint:         endpoint,
		S3ForcePathStyle: s3ForcePathStyle,
	})

	var req *aws.Request
	req, _ = svc.GetObjectRequest(&s3.GetObjectInput{
		Bucket: &bucket,
		Key:    &key,
	})

	url, err := req.Presign(time.Duration(expiry) * time.Second)
	if err != nil {
		return "", err
	}
	return url, nil
}