Javascript 使用aws sdk进行放大和反应
我在使用Amplify构建的React应用程序中使用AWS SDK for Javascript时遇到问题。在成功地将映像上载到S3之后,我正在尝试写入DynamoDB表。图像上载目前可以工作,但用于写入测试DynamoDB表的SDK方法不能工作Javascript 使用aws sdk进行放大和反应,javascript,reactjs,amazon-web-services,aws-sdk,aws-amplify,Javascript,Reactjs,Amazon Web Services,Aws Sdk,Aws Amplify,我在使用Amplify构建的React应用程序中使用AWS SDK for Javascript时遇到问题。在成功地将映像上载到S3之后,我正在尝试写入DynamoDB表。图像上载目前可以工作,但用于写入测试DynamoDB表的SDK方法不能工作 import React, { Component } from 'react'; import { Auth } from 'aws-amplify' import { withAuthenticator } from 'aws-amplify-re
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"
}
}
*/
});
})
}
句柄submit的第一部分可以工作,直到Storage.put,但是DynamoDB putItem方法似乎没有做任何事情,尽管进行了编译。有人能给我指出将这些结合使用的正确方向吗?您可以在dynamo db API前面使用AppSync或API网关之类的东西 你可以看看: 对于使用AppSync和GraphQL的示例(由dynamodb支持) 或: 它使用API网关调用REST API来查询dynamodb。首先调用
amplify add storage
并选择NoSql Database
,然后使用amplify设置表,然后调用amplify add API
即可轻松完成此操作。它将为您添加一组文件作为后端,您可以调用这些文件进行调用去你的发电机桌
但是,在现有代码中,您可能希望尝试在react中使用带有适当import语句的sdk,比如import AWS from'AWS sdk'
查看它是否有效