因此,我目前正在尝试使用API网关将数据发布到DynamoDB。当我对其进行测试时,它以200
状态返回-但我的DynamoDB从未更新。
这是我的axios帖子请求:
axios.post('https://1eugzswetg.execute-api.us-west-1.amazonaws.com/beta/users', {
email: "test@gmail.com",
salt: "123",
sha512: "123"
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})
这是我使用该API网关访问的Lambda函数:
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient({ region: 'us-west-1' });
exports.handler = async function(event, ctx, callback) {
var params = {
Item: {
email: event.email,
salt: event.salt,
sha512: event.sha512
},
TableName: 'users'
}
try {
const data = await dynamoDb.put(params).promise()
console.log({ statusCode: 200, body: { params, data }})
} catch (err) {
console.log(err)
}
}
这是我的axios呼叫后记录的内容:
{ status: 200,
statusText: 'OK',
headers:
{ date: 'Thu, 28 Feb 2019 02:41:01 GMT',
'content-type': 'application/json',
'content-length': '4',
connection: 'close',
'x-amzn-requestid': '49249c4f-3b02-11e9-af6d-819f17575656',
'x-amz-apigw-id': 'VyiJmFI6SK4FgYQ=',
'x-amzn-trace-id': 'Root=1-5c774a3d-2fc47364e1170574a6f083c4;Sampled=0' },
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
'User-Agent': 'axios/0.18.0',
'Content-Length': 54 },
method: 'post',
url:
'https://1eugzswetg.execute-api.us-west-1.amazonaws.com/beta/users',
data: '{"email":"test@gmail.com","salt":"123","sha512":"123"}' },
request:
ClientRequest {
domain: null,
_events:
{ socket: [Function],
abort: [Function],
aborted: [Function],
error: [Function],
timeout: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: '1eugzswetg.execute-api.us-west-1.amazonaws.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host: '1eugzswetg.execute-api.us-west-1.amazonaws.com',
_readableState: [ReadableState],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 7,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: '1eugzswetg.execute-api.us-west-1.amazonaws.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host: '1eugzswetg.execute-api.us-west-1.amazonaws.com',
_readableState: [ReadableState],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 7,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
_header:
'POST /beta/users HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nUser-Agent: axios/0.18.0\r\nContent-Length: 54\r\nHost: 1eugzswetg.execute-api.us-west-1.amazonaws.com\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/beta/users',
_ended: true,
res:
IncomingMessage {
_readableState: [ReadableState],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
connection: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 200,
statusMessage: 'OK',
client: [TLSSocket],
_consuming: false,
_dumped: false,
req: [Circular],
responseUrl:
'https://1eugzswetg.execute-api.us-west-1.amazonaws.com/beta/users',
redirects: [] },
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable:
Writable {
_writableState: [WritableState],
writable: true,
domain: null,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 54,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: [Circular],
_currentUrl:
'https://1eugzswetg.execute-api.us-west-1.amazonaws.com/beta/users' },
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]:
{ accept: [Array],
'content-type': [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array] } },
data: null }
有人知道为什么我的DynamoDB没有得到更新吗?当我在API Gateway控制台中对其进行测试时,它可以工作。这是我在API Gateway控制台中对其进行测试时发生的屏幕截图
答案 0 :(得分:0)
如果它是通过API网关控制台运行的,那么它是客户端问题,还是未部署到api网关阶段的最新版本?
答案 1 :(得分:0)
提示:
您可以尝试使用Amazon cloudwatch服务,并查看其功能内部发生的情况。
您确定您的用户(IAM)拥有权限吗? Access Keys for IAM Users
您必须在此函数中返回如下内容:
export async function ping(event, context) {
...
...
...
return Promise((resolve,reject) => {
return resolve({
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": null,
"isBase64Encoded": false
})
})
}
我为您准备了一个基本示例(有效的)putItem实现:
您必须查看的文件:
重要:您的系统中必须具有AWS凭证文件。例如:〜/ .aws / credentials
祝你好运!