title: How to use gcp signed url with custom headers in Go
body: <p>I am trying to set the x-goog-meta-reference in the header section dynamically so i want to pass an argument called reference in the function signature and assign that to the x-goog-meta-reference in the header. See <code>x-goog-meta-reference</code> in my code sample below. I followed <a href="https://cloud.google.com/storage/docs/authentication/canonical-requests#required-query-parameters" rel="nofollow noreferrer">this</a> link regarding canonical requests.</p>
  <p>My sample code is from <a href="https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-object-go" rel="nofollow noreferrer">here</a> but it is the edited version of my prod code.</p>
  <pre><code>func GenerateSignedURL(ctx context.Context, bucket string, key string, expiration time.Time,
  ) (string, error) {
      gcsClient, err := storage.NewClient(ctx)
      if err != nil {
          return &quot;&quot;, fmt.Errorf(&quot;storage.NewClient: %v&quot;, err)
      }
      defer gcsClient.Close()
  
      storage.SignedURL()
      opts := &amp;storage.SignedURLOptions{
          Scheme:      storage.SigningSchemeV4,
          Method:      &quot;PUT&quot;,
          ContentType: &quot;text/csv&quot;,
          Headers:     []string{
              &quot;x-goog-meta-reference: xxx&quot;, // &lt;&lt; I want xxx value to be whatever I pass to this function as an arg
          },
          Expires:     expiration,
      }
  
      url, err := gcsClient.Bucket(bucket).SignedURL(key, opts)
      if err != nil {
          log.WithContext(ctx).Warn(&quot;Failed to generate a GCS signed URL&quot;)
          return &quot;&quot;, err
      }
  
      return url, nil
  }
  </code></pre>
  <p>I tried looking at some examples but all of them are s3 and I did not run into any gcp sample code. However, I did find <a href="https://github.com/googleapis/google-cloud-go/issues/1511" rel="nofollow noreferrer">this</a> issue but i was not able to figure it out myself.</p>