使用 golang 从 DynamoDB 获取记录时出现紧急运行时错误

Posted

技术标签:

【中文标题】使用 golang 从 DynamoDB 获取记录时出现紧急运行时错误【英文标题】:panic runtime error while fetching records from DynamoDB using golang 【发布时间】:2017-04-03 02:33:19 【问题描述】:

我在访问 aws-sdk-go 时遇到问题,我正在尝试使用 aws-sdk-go 连接到 dynamo DB。我能够建立与数据库的连接并且插入操作工作正常,但是当我尝试从表中检索数据时,我收到“panic runtime error invalid memory address or nil pointer dereference golang”错误。请找到我使用的代码。我对这两种技术都很陌生。

package main 
import (
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/aws/credentials"
  "github.com/aws/aws-sdk-go/service/dynamodb"
  "github.com/user/dynamo_connect/data"
  "github.com/user/insert_api/read"
)
import (
  "fmt"
  "log"
  "net/http"
  "crypto/md5"
  "encoding/hex"
  "io"
  "time"
  "github.com/gorilla/mux"
)

func main() 
  router := mux.NewRouter().StrictSlash(true)
  router.HandleFunc("/get", GetCampaignRecord)
  log.Fatal(http.ListenAndServe(":8080", router))


func GetCampaignRecord(w http.ResponseWriter, r *http.Request) 
  var testCredentials = credentials.NewStaticCredentials("AKIxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "")
  sess, err := session.NewSession()
  svc := dynamodb.New(sess, &aws.Config
    Region: aws.String("ap-south-1"),
    Credentials: testCredentials,
  )

  resp, err := read.GetCampaignData(svc)
  if err != nil 
    fmt.Println("An error occurred while writing to the Employee table")
    fmt.Println(err.Error())
  

  if resp != nil 
    fmt.Println("An error occurred while writing to the Employee table")
    fmt.Println(err.Error())
  

包读取

    package read
    import "github.com/aws/aws-sdk-go/service/dynamodb"
    import "github.com/aws/aws-sdk-go/aws"

    func GetCampaignData(svc *dynamodb.DynamoDB) (*dynamodb.BatchGetItemOutput, error) 
      params := &dynamodb.BatchGetItemInput
        RequestItems: map[string]*dynamodb.KeysAndAttributes
            "employee": 
                Keys: []map[string]*dynamodb.AttributeValue
                    
                        "employee_id": 
                            S:    aws.String("1"),
                        ,
                    ,
                ,
                ProjectionExpression: aws.String("employee_id, employee_name"),
            ,
        ,
        ReturnConsumedCapacity: aws.String("TOTAL"),
    

    return svc.BatchGetItem(params)
    

我得到的完全错误

http: panic serving 127.0.0.1:36034: runtime error: invalid memory address or nil pointer dereference goroutine 18 [运行]: net/http.(*conn).serve.func1(0xc820102000) /usr/local/go/src/net/http/server.go:1389 +0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 +0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0, 0xc82012a020, 0x1, 0x1, 0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 +0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0, 0x935410, 0x8, 0xc82012a020, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, ...) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 +0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248, 0x0, 0xc82012a020, 0x1, 0x1, 0xc820112260) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 +0x78 main.GetCampaignRecord(0x7f20477cd058, 0xc820122000, 0xc82010e000) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 +0x329 net/http.HandlerFunc.ServeHTTP(0xa07d38, 0x7f20477cd058, 0xc820122000, 0xc82010e000) /usr/local/go/src/net/http/server.go:1618 +0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0, 0x7f20477cd058, 0xc820122000, 0xc82010e000) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 +0x2a8 网络/http.serverHandler.ServeHTTP(0xc82006e180, 0x7f20477cd058, 0xc820122000, 0xc82010e000) /usr/local/go/src/net/http/server.go:2081 +0x19e net/http.(*conn).serve(0xc820102000) /usr/local/go/src/net/http/server.go:1472 +0xf2e 由 net/http.(*Server).Serve 创建 /usr/local/go/src/net/http/server.go:2137 +0x44e 2016/11/19 13:31:34 http:恐慌服务 127.0.0.1:36035:运行时错误:无效的内存地址或 nil 指针取消引用 goroutine 5 [运行]: net/http.(*conn).serve.func1(0xc82006e200) /usr/local/go/src/net/http/server.go:1389 +0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 +0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0, 0xc82012a038, 0x1, 0x1, 0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 +0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0, 0x935410, 0x8, 0xc82012a038, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, ...) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 +0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248, 0x0, 0xc82012a038, 0x1, 0x1, 0xc820112540) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 +0x78 main.GetCampaignRecord(0x7f20477cd058, 0xc8201224e0, 0xc8200d20e0) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 +0x329 net/http.HandlerFunc.ServeHTTP(0xa07d38, 0x7f20477cd058, 0xc8201224e0, 0xc8200d20e0) /usr/local/go/src/net/http/server.go:1618 +0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0, 0x7f20477cd058, 0xc8201224e0, 0xc8200d20e0) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 +0x2a8 网络/http.serverHandler.ServeHTTP(0xc82006e180, 0x7f20477cd058, 0xc8201224e0, 0xc8200d20e0) /usr/local/go/src/net/http/server.go:2081 +0x19e net/http.(*conn).serve(0xc82006e200) /usr/local/go/src/net/http/server.go:1472 +0xf2e 由 net/http.(*Server).Serve 创建 /usr/local/go/src/net/http/server.go:2137 +0x44e 2016/11/19 13:31:34 http:恐慌服务 127.0.0.1:36036:运行时错误:无效的内存地址或 nil 指针取消引用 goroutine 34 [运行]: net/http.(*conn).serve.func1(0xc820114280) /usr/local/go/src/net/http/server.go:1389 +0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 +0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0, 0xc82012a058, 0x1, 0x1, 0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 +0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0, 0x935410, 0x8, 0xc82012a058, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, ...) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 +0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248, 0x0, 0xc82012a058, 0x1, 0x1, 0xc820112840) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 +0x78 main.GetCampaignRecord(0x7f20477cd058, 0xc820122820, 0xc82016c000) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 +0x329 net/http.HandlerFunc.ServeHTTP(0xa07d38, 0x7f20477cd058, 0xc820122820, 0xc82016c000) /usr/local/go/src/net/http/server.go:1618 +0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0, 0x7f20477cd058, 0xc820122820, 0xc82016c000) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 +0x2a8 网络/http.serverHandler.ServeHTTP(0xc82006e180, 0x7f20477cd058, 0xc820122820, 0xc82016c000) /usr/local/go/src/net/http/server.go:2081 +0x19e net/http.(*conn).serve(0xc820114280) /usr/local/go/src/net/http/server.go:1472 +0xf2e 由 net/http.(*Server).Serve 创建 /usr/local/go/src/net/http/server.go:2137 +0x44e

【问题讨论】:

【参考方案1】:

我查看了您的代码,在我看来,您的数据库连接存在问题。您正在尝试连接到一些不同的区域。因此,请先重新检查您所在的地区。它位于您的 dynamodb 的 AWS 账户的 URL 中。(例如 us-east-2)。您还可以通过在终端中键入以下命令来检查或配置您的区域:aws configure。然后它会要求您输入您的访问密钥、密钥、区域名称等。在此处输入所有详细信息。而已。干杯。

【讨论】:

感谢@Geek_KK,更改区域解决了这个问题,实际上我的一位同事试图访问这些 AWS 凭证,他不小心将区域设置为默认值,而我使用的是 'ap-south-1' 这个地区。再次感谢

以上是关于使用 golang 从 DynamoDB 获取记录时出现紧急运行时错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Appsync 中的解析器获取 dynamodb 中存在的记录总数

将 golang S2 Geometry 库与 dynamodb 一起使用

dynamodb 查询我试图使用 laravel 从 dynamodb 获取一些数据

golang DynamoDB sdk AccessDeniedException

如何在 Lambda 中使用 python 从 dynamoDB 获取/获取某些列?

如何从 aws 控制台将 dynamoDB 表的全部记录导出到 CSV?