doukan1258 2016-11-19 08:13
浏览 119
已采纳

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

I am facing issue while accessing aws-sdk-go, I am trying to connect to dynamo DB using aws-sdk-go. I am able to establish connection to database and insert operation is working fine, but when I am trying to retrieve data from table I am getting "panic runtime error invalid memory address or nil pointer dereference golang" error. Please find code I used. I am new to both technologies.

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

    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)
    }

Full Error I am getting

http: panic serving 127.0.0.1:36034: runtime error: invalid memory address or nil pointer dereference goroutine 18 [running]: net/http.(*conn).serve.func1(0xc820102000) /usr/local/go/src/net/http/server.go:1389 +0xc1 panic(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 net/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 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2137 +0x44e 2016/11/19 13:31:34 http: panic serving 127.0.0.1:36035: runtime error: invalid memory address or nil pointer dereference goroutine 5 [running]: net/http.(*conn).serve.func1(0xc82006e200) /usr/local/go/src/net/http/server.go:1389 +0xc1 panic(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 net/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 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2137 +0x44e 2016/11/19 13:31:34 http: panic serving 127.0.0.1:36036: runtime error: invalid memory address or nil pointer dereference goroutine 34 [running]: net/http.(*conn).serve.func1(0xc820114280) /usr/local/go/src/net/http/server.go:1389 +0xc1 panic(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 net/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 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2137 +0x44e

  • 写回答

1条回答 默认 最新

  • duanchun6148 2016-11-19 10:51
    关注

    I have gone through your code and it seems to me that, there is problem with your database connectivity. You are trying to connect to maybe some different region. So, please first recheck your region. It is there in the URL of AWS account of your dynamodb.(e.g us-east-2). You can also check or configure your region by typing this command into the terminal : aws configure . Then it will ask you to enter your access key, secret key, region name etc .Enter all the details here. That's it. Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!