簡體   English   中英

使用帶放大和反應的aws-sdk

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM