亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

lambda 多部分/文件上傳

lambda 多部分/文件上傳

Go
SMILET 2022-09-12 21:18:55
我需要對使用 Go 通過 AWS Lambda 上傳的上傳文件的內容進行一些簡單的操作,但不確定如何解析接收內容,因為我是 Go 的新手。到目前為止,我發現的解決方案與http包和多部分表單功能有關。type Request events.APIGatewayProxyRequestfunc Handler(ctx context.Context, req Request) (Response, error) {fmt.Println(req.Body)....}這就是我的請求正文的樣子------WebKitFormBoundaryx0SVDelfa90Fi5UoContent-Disposition: form-data; name="file"; filename="upload.txt"Content-Type: text/plainthis is content------WebKitFormBoundaryx0SVDelfa90Fi5Uo--我的請求是 的實例。APIGatewayProxyRequest我想知道是否有可能獲得一個自定義結構,從中我可以訪問f.e.等數據。customStruct.content => "this is content"customStruct.fileName => upload.txtcustomStruct.fileExtension => txt
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

這有3個部分:

  1. 創建一個從multipart.Readerevents.APIGatewayProxyRequest

  2. 獲取啞劇部分

  3. 提取啞劇部件值

步驟 1:創建multipart.Reader

取一個 和 字符串,如簽名所示:multipart.NewReaderio.Readerboundary

func NewReader(r io.Reader, boundary string) *Reader

為此,您需要從 HTTP 請求標頭中提取邊界字符串,這可以使用 來完成。Content-Typemime.ParseMediaType

執行此操作的一種簡單方法是從具有以下簽名的 go-awslambda 包中調用:NewReaderMultipart

func NewReaderMultipart(req events.APIGatewayProxyRequest) (*multipart.Reader, error)

步驟 2:獲取 MIME 部件

獲得 后,導航 MIME 消息,直到找到所需的 MIME 部分。mime.Reader

在這里的示例中,只有一個部分,因此您可以簡單地調用:

part, err := reader.NextPart()

步驟 3:提取 MIME 部件值

獲得 MIME 部件后,可以提取所需的值。

步驟 3.1:內容

content, err := io.ReadAll(part)

步驟 3.2:文件名

從 MIME 部件獲取文件名,如下所示:

filename := part.FileName()

步驟 3.3: 文件擴展名

叫。這將在擴展中添加前導句點,但這可以很容易地刪除。path/filepath.Ext.

ext := filepath.Ext(part.FileName())

總結

您可以按如下方式進行組合:

import (

    "context"

    "encoding/json"

    "io"


    "github.com/aws/aws-lambda-go/events"

    "github.com/grokify/go-awslambda"

)


type customStruct struct {

    Content       string

    FileName      string

    FileExtension string

}


func handleRequest(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    res := events.APIGatewayProxyResponse{}

    r, err := awslambda.NewReaderMultipart(req)

    if err != nil {

        return res, err

    }

    part, err := r.NextPart()

    if err != nil {

        return res, err

    }

    content, err := io.ReadAll(part)

    if err != nil {

        return res, err

    }

    custom := customStruct{

        Content:       string(content),

        FileName:      part.FileName(),

        FileExtension: filepath.Ext(part.FileName())}


    customBytes, err := json.Marshal(custom)

    if err != nil {

        return res, err

    }


    res = events.APIGatewayProxyResponse{

        StatusCode: 200,

        Headers: map[string]string{

            "Content-Type": "application/json"},

        Body: string(customBytes)}

    return res, nil

}


查看完整回答
反對 回復 2022-09-12
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

上面的答案看起來是正確的方法,但是如果你很懶惰(正如我:p),你可以創建一個HTTP請求,讓本機lib為你工作:


httpReq, err := http.NewRequestWithContext(ctx, req.HTTPMethod, reqURL.String(), strings.NewReader(req.Body))

if err != nil {

    return nil, errors.New("failed to convert a lambda req into a http req")

}


// some headers may be important, let get all of them, just in case

for name, value := range req.Headers {

    httpReq.Header.Add(name, value)

}



// from here you can use httpReq.FormFile() to read the file


查看完整回答
反對 回復 2022-09-12
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號