問題:
如何執行預簽POST upload 使用GoPOST upload將檔案上傳到AWS S3 儲存桶,而不使用傳統的預簽PUT方法?
解決方案:
要執行預先簽署POST 上傳,請依照以下步驟操作:
建構並發布多部分錶單資料: 使用下列欄位建立多部分錶單資料請求:
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 }
以上是如何使用Go實作預簽POST檔上傳到AWS S3?的詳細內容。更多資訊請關注PHP中文網其他相關文章!