简体   繁体   English

如何在 Go 中向 AWS S3 预签名 URL 添加标头?

[英]How can I add a header to an AWS S3 presigned URL in Go?

I'm uploading a file to an AWS S3 bucket using a presigned URL.我正在使用预签名 URL 将文件上传到 AWS S3 存储桶。 This works fine, but if I try to add a x-amz-tagging header I get the error "There were headers present in the request which were not signed".这工作正常,但如果我尝试添加x-amz-tagging标头,我会收到错误“请求中存在未签名的标头”。

The backend generating the presigned URL is written in Go:生成预签名 URL 的后端是用 Go 编写的:

// Upload generates a new URL where a file can be uploaded
func (s *S3) Upload(key string, c Config) (string, error) {
    req, _ := s.client.PutObjectRequest(&s3.PutObjectInput{
        Bucket: aws.String(s.bucketName),
        Key:    aws.String(key),
    })

    return req.Presign(c.ExpiresIn)
}

The answer to S3 presigned upload url error suggests that we need to declare the header as part of the presigned URL. S3 预签名上传 url 错误的答案表明我们需要将标头声明为预签名 URL 的一部分。 How can I add a header declaration to this?如何为此添加标头声明? The examples given on Creating Pre-Signed URLs for Amazon S3 Buckets don't cover this. 为 Amazon S3 存储桶创建预签名 URL 中给出的示例并未涵盖这一点。

If i remember correctly, you can add metadata like this:如果我没记错的话,您可以添加这样的元数据:

  'Bucket': 'bucket',
  'Key': 'signed.json',
  'Metadata': {
    'x-amz-tagging': 'whatever'
  },

So it'll look something like this:所以它看起来像这样:

// Upload generates a new URL where a file can be uploaded
func (s *S3) Upload(key string, c Config) (string, error) {
    req, _ := s.client.PutObjectRequest(&s3.PutObjectInput{
        Bucket: aws.String(s.bucketName),
        Key:    aws.String(key),
        Body: {
                'x-amz-tagging': 'whatever'
       },
    })

    return req.Presign(c.ExpiresIn)
}

I wrote this on the fly, so it might not work and need a bit of tweaking.我是即时写的,所以它可能不起作用,需要一些调整。 Refer to the docs - but it should look like this.请参阅文档 - 但它应该是这样的。 Test it out and let me know.测试一下,让我知道。

Read more here: https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#PutObjectInput在此处阅读更多信息: https : //docs.aws.amazon.com/sdk-for-go/api/service/s3/#PutObjectInput

The presigned URL request just needs the tags under the Tagging field.预签名 URL 请求只需要 Tagging 字段下的标签。 When you use this URL to make a request, that's when you pass the header with "x-amz-tagging" as the key and the tags (eg, "temp=true&public=yes").当您使用此 URL 发出请求时,即传递以“x-amz-tagging”作为键和标签的标头(例如,“temp=true&public=yes”)。

Below is an example of uploading an image with tags (through the headers) using a presigned url that was also requested with tags (with Tagging).下面是一个使用预先签名的 url 上传带有标签的图像的示例(通过标题),该 url 也是使用标签(带标签)请求的。

  const uploadImage = async (filepath, presignedURL) => {
    const headers = new Headers();
    headers.append('x-amz-tagging', 'temp=true&public=yes');
    const response = await fetch(filepath);
    const blob = await response.blob();
    return await fetch(
      presignedURL,
      {
        method: 'PUT',
        body: blob,
        headers,
      },
    );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM