aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

[Enhancement]: Adding custom HTTP headers for s3 pre-signed url requests in golang

Open sanjanasekar opened this issue 3 years ago • 3 comments

Background story

We are looking for guidance to build s3 pre-signed URL with custom headers. This is needed for additional security purposes for making requests to s3 to an internal security purpose.

The current example doesn't provide any info on this. Would the following help add these headers, if not please help provide guidance on this ?

                func AddCustomHeadersToRequest(req *request.Request) *request.Request {
                        xAmzMyKey1 := "X-Amz-Foo1"
                        xAmzMyKey1 := "X-Amz-Foo2"
                        myValue1 := "Bar1"
                        myValue2 := "Bar2"
	                headers := req.HTTPRequest.Header
	                headers[xAmzMyKey1] = []string{myValue1}
	                headers[xAmzMyKey2] = []string{myValue2}
	                return req
                }
                type S3Client struct {
	                Client s3iface.S3API
                }

		s3Req, _ := s.Client.GetObjectRequest(&s3.GetObjectInput{
			Bucket: aws.String(containerLogBucket),
			Key:    aws.String(containerLogKey)})
		AddCustomHeadersToRequest(s3Req)
		// Presigned URL timeout set to expiration value
		if presignUrlStr, err := s3Req.Presign(s3PresignedUrlExpirationDuration); err != nil {
			log.Errorf("Presigned URL creation failed: %v", err)
			return err
		} else {
			log.Infof("Presigned URL: %v", presignUrlStr)
			http.Redirect(resp, req, presignUrlStr, http.StatusFound)
		}

What does this example accomplish?

Add custom headers while generating pre-signed URL for S3.

Which AWS service(s)?

s3

Which AWS SDKs or tools?

  • [ ] All languages
  • [ ] .NET
  • [X] Go (v2)
  • [ ] Java
  • [ ] Java (v2)
  • [ ] JavaScript
  • [ ] JavaScript (v3)
  • [ ] Kotlin
  • [ ] PHP
  • [ ] Python
  • [ ] Ruby
  • [ ] Rust
  • [ ] Swift
  • [ ] Not applicable

Are there existing code examples to leverage?

Existing PresignedURL example

Do you have any reference code?

func AddCustomHeadersToRequest(req *request.Request) *request.Request {
                        xAmzMyKey1 := "X-Amz-Foo1"
                        xAmzMyKey1 := "X-Amz-Foo2"
                        myValue1 := "Bar1"
                        myValue2 := "Bar2"
	                headers := req.HTTPRequest.Header
	                headers[xAmzMyKey1] = []string{myValue1}
	                headers[xAmzMyKey2] = []string{myValue2}
	                return req
                }
                type S3Client struct {
	                Client s3iface.S3API
                }

		s3Req, _ := s.Client.GetObjectRequest(&s3.GetObjectInput{
			Bucket: aws.String(containerLogBucket),
			Key:    aws.String(containerLogKey)})
		AddCustomHeadersToRequest(s3Req)
		// Presigned URL timeout set to expiration value
		if presignUrlStr, err := s3Req.Presign(s3PresignedUrlExpirationDuration); err != nil {
			log.Errorf("Presigned URL creation failed: %v", err)
			return err
		} else {
			log.Infof("Presigned URL: %v", presignUrlStr)
			http.Redirect(resp, req, presignUrlStr, http.StatusFound)
		}

sanjanasekar avatar Dec 08 '22 18:12 sanjanasekar

@Laren-AWS To investigate further

brmur avatar Dec 15 '22 18:12 brmur

Marked stale by the Shirriff. Notifying @awsdocs/aws-sdk-docs-code-maintainers

github-actions[bot] avatar Jan 06 '23 01:01 github-actions[bot]

The attached code is for SDK for Go v1, which we don't support any longer for code examples. My understanding is that for Go v2 you can add custom headers to the HTTP request using standard mechanisms of the net/http library. This doesn't meet the bar for new code examples. We can reopen if feedback says different.

Laren-AWS avatar Jan 22 '24 19:01 Laren-AWS