连接 AWS Lambda 和 MongDB Atlas 的基本示例问题

问题描述 投票:0回答:0

我正在尝试按照那里的 MondoDB 教程连接 fram AWS Lambda 来连接到 MongoDB Atlas:https://www.mongodb.com/developer/languages/go/interact-aws-lambda-function-去/

但是我在测试Lambda函数的时候出错了

经过多次测试,我认为它来自

opts.SetLimit
.

main.go

package main

import (
    "context"
    "log"
    "os"

    "github.com/aws/aws-lambda-go/lambda"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

type EventInput struct {
    Limit int64 `json:"limit"`
}

type Movie struct {
    ID    primitive.ObjectID `bson:"_id" json:"_id"`
    Title string             `bson:"title" json:"title"`
    Year  int32              `bson:"year" json:"year"`
}

var client, err = mongo.Connect(context.Background(), options.Client().ApplyURI(os.Getenv("ATLAS_URI")))

func HandleRequest(ctx context.Context, input EventInput) ([]Movie, error) {
    if err != nil {
        return nil, err
    }

    collection := client.Database("sample_mflix").Collection("movies")

    opts := options.Find()

    if input.Limit != 0 {
        log.Printf("limit : %d\n", input.Limit)
        log.Printf("limit before : %d\n", opts.Limit)

        opts = opts.SetLimit(input.Limit)

        log.Printf("limit : %d\n", input.Limit)
        log.Printf("limit after : %d\n", opts.Limit)
    }
    log.Println("opts : ", opts)

    cursor, err := collection.Find(context.Background(), bson.M{}, opts)
    if err != nil {
        return nil, err
    }
    var movies []Movie
    if err = cursor.All(context.Background(), &movies); err != nil {
        return nil, err
    }

    return movies, nil
}

func main() {
    lambda.Start(HandleRequest)
}

go.mod

module aws-lambdaexample

go 1.20

require (
    github.com/aws/aws-lambda-go v1.41.0 // indirect
    github.com/golang/snappy v0.0.1 // indirect
    github.com/joho/godotenv v1.5.1 // indirect
    github.com/klauspost/compress v1.13.6 // indirect
    github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
    github.com/pkg/errors v0.9.1 // indirect
    github.com/xdg-go/pbkdf2 v1.0.0 // indirect
    github.com/xdg-go/scram v1.1.1 // indirect
    github.com/xdg-go/stringprep v1.0.3 // indirect
    github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
    go.mongodb.org/mongo-driver v1.11.6 // indirect
    golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
    golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
    golang.org/x/text v0.3.7 // indirect
)

lambda 函数日志

START RequestId: 9b96311b-2211-4d94-82c6-3bc0f2fd00a2 Version: $LATEST
2023/05/10 08:32:27 limit : 3
2023/05/10 08:32:27 limit before : 0
2023/05/10 08:32:27 limit : 3
2023/05/10 08:32:27 limit after : 824635785848
2023/05/10 08:32:27 opts :  &{<nil> <nil> <nil> <nil> <nil> <nil>
<nil> 0xc0001f8278 <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>
<nil> <nil> <nil> <nil> <nil>}
2023-05-10T08:32:29.025Z 9b96311b-2211-4d94-82c6-3bc0f2fd00a2 Task timed out after 2.00 seconds

我做错了什么?

go aws-lambda mongodb-atlas
© www.soinside.com 2019 - 2024. All rights reserved.