第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

lambda 多部分/文件上傳

lambda 多部分/文件上傳

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

2 回答

?
開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

這有3個(gè)部分:

  1. 創(chuàng)建一個(gè)從multipart.Readerevents.APIGatewayProxyRequest

  2. 獲取啞劇部分

  3. 提取啞劇部件值

步驟 1:創(chuàng)建multipart.Reader

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

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

為此,您需要從 HTTP 請(qǐng)求標(biāo)頭中提取邊界字符串,這可以使用 來(lái)完成。Content-Typemime.ParseMediaType

執(zhí)行此操作的一種簡(jiǎn)單方法是從具有以下簽名的 go-awslambda 包中調(diào)用:NewReaderMultipart

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

步驟 2:獲取 MIME 部件

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

在這里的示例中,只有一個(gè)部分,因此您可以簡(jiǎn)單地調(diào)用:

part, err := reader.NextPart()

步驟 3:提取 MIME 部件值

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

步驟 3.1:內(nèi)容

content, err := io.ReadAll(part)

步驟 3.2:文件名

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

filename := part.FileName()

步驟 3.3: 文件擴(kuò)展名

叫。這將在擴(kuò)展中添加前導(dǎo)句點(diǎn),但這可以很容易地刪除。path/filepath.Ext.

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

總結(jié)

您可以按如下方式進(jìn)行組合:

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

}


查看完整回答
反對(duì) 回復(fù) 2022-09-12
?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

上面的答案看起來(lái)是正確的方法,但是如果你很懶惰(正如我:p),你可以創(chuàng)建一個(gè)HTTP請(qǐng)求,讓本機(jī)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


查看完整回答
反對(duì) 回復(fù) 2022-09-12
  • 2 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)