在使用Amplify构建的React应用程序中使用适用于Javascript的AWS开发工具包时遇到了麻烦。成功将图像上传到S3后,我尝试写入DynamoDB表。当前可以上传图片,但无法写入测试DynamoDB表的SDK方法。
import React, { Component } from 'react';
import { Auth } from 'aws-amplify'
import { withAuthenticator } from 'aws-amplify-react'
import { Storage } from 'aws-amplify';
const aws = require('aws-sdk'); //"^2.2.41"
handleSubmit = (event) => {
event.preventDefault();
if (this.state.file == null) {
alert("File Not Chosen")
}
else {
const file = this.state.file;
Storage.put(this.state.name, file, {
contentType: 'image',
bucket:'myapp-20181030214040-deployment'
})
.then (result => console.log(result))
.catch(err => console.log(err));
}
Auth.currentCredentials()
.then(credentials => {
const dynamodb = new aws.DynamoDB({
apiVersion: '2012-08-10',
credentials: Auth.essentialCredentials(credentials)
});
let params = {
Item: {
"testKey": {
S: "test1"
}
},
ReturnConsumedCapacity: "TOTAL",
TableName: "test"
};
dynamodb.putItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
/*
data = {
ConsumedCapacity: {
CapacityUnits: 1,
TableName: "Music"
}
}
*/
});
})
}
句柄的第一部分提交工作到Storage.put为止,但是DynamoDB putItem方法尽管进行了编译,但似乎并未执行任何操作。谁能指出我将它们一起使用的正确方向?
答案 0 :(得分:0)
您可以在dynamo db api之前使用AppSync或API Gateway之类的东西。
您可以看一下:https://github.com/aws-samples/aws-mobile-appsync-events-starter-react
对于使用AppSync和GraphQL(由dynamodb支持)的示例
或:https://github.com/aws-samples/aws-mobile-react-sample
使用API Gateway调用REST api来查询dynamodb。这可以轻松完成,方法是先调用amplify add storage
并选择NoSql Database
,然后使用amplify设置表,然后调用{{1} }它将为您添加一堆文件作为后端,您可以调用这些文件来调用dynamo表。
但是在现有代码中,您可能还想尝试使用带有适当import语句的sdk来进行类似amplify add api
的反应,以查看其是否有效。