How to Achieve Pre-signed POST File Uploads to AWS S3 using Go?

Mary-Kate Olsen
Release: 2024-11-25 06:02:11
Original
620 people have browsed it

How to Achieve Pre-signed POST File Uploads to AWS S3 using Go?

Pre-Signed POST Upload to AWS S3 in Go

Problem:

How to perform a Pre-signed POST upload to upload files to an AWS S3 bucket using Go, without using the traditional Pre-signed PUT method?

Solution:

To perform a Pre-signed POST upload, follow these steps:

  1. Configure the S3 Bucket for Public Downloads: Set the bucket policy to allow public downloads only.
  2. Create a POST Policy: Generate a POST policy that allows upload to a specific key, bucket, and grants public-read access.
  3. Generate and Sign the Policy: Use the S3 bucket owner's credentials to generate and sign the POST policy, encoding it in base64 and hex.
  4. Construct and POST Multipart Form Data: Create a multipart form data request with the following fields:

    • (key) The name of the file to upload
    • (policy) The base64-encoded POST policy
    • (signature) The hex-encoded signature of the policy
    • (x-amz-credential) The AWS credentials used to sign the policy
    • (x-amz-algorithm) The algorithm used for signing the policy (AWS4-HMAC-SHA256)
    • (x-amz-date) The date used for signing the policy

Example Code in Go:

import (
    "bytes"
    "fmt"
    "io"
    "mime/multipart"
    "net/http"
    "strings"
)

// Fields represents the fields to be uploaded in the multipart form data request.
type Fields struct {
    Key, Value string
}

// Upload performs a Pre-signed POST upload using the provided URL and fields.
func Upload(url string, fields []Fields) error {
    var b bytes.Buffer
    w := multipart.NewWriter(&b)
    for _, f := range fields {
        fw, err := w.CreateFormField(f.Key)
        if err != nil {
            return err
        }
        if _, err := io.WriteString(fw, f.Value); err != nil {
            return err
        }
    }
    w.Close()

    req, err := http.NewRequest("POST", url, &b)
    if err != nil {
        return err
    }
    req.Header.Set("Content-Type", w.FormDataContentType())

    client := &http.Client{}
    res, err := client.Do(req)
    if err != nil {
        return err
    }
    if res.StatusCode != http.StatusOK {
        err = fmt.Errorf("bad status: %s", res.Status)
    }
    return nil
}
Copy after login

The above is the detailed content of How to Achieve Pre-signed POST File Uploads to AWS S3 using Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template