[英]Using aws-sdk with amplify and React
我在使用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-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方法似乎没有做任何事情,尽管编译。 任何人都能指出我在一起使用这些方向的正确方向吗?
您可以在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网关调用REST api来查询dynamodb。这可以通过首先调用amplify add storage
并选择NoSql Database
并使用放大设置表然后调用amplify add api
来轻松完成,它将添加一堆文件作为你可以打电话给你的发电机后台。
但是在现有代码中你可能想尝试使用带有正确import语句的sdk,就像import AWS from 'aws-sdk';
一样import AWS from 'aws-sdk';
看它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.