2017-03-03 54 views
0

我創建了一個用於連接我的網頁的AWS賬戶。我有2個Java腳本函數來運行AWS dynamoDB。爲什麼在JavaScript SDK中使用密鑰和secert acess密鑰失敗?

我可以通過直接accessKeyId和secretAccessKey連接數據庫。這樣的代碼

(function() { 

// Setup basic AWS configuration 
    AWS.config.update({ 
    region: "us-west-2", 
    accessKeyId: "MY_ACESS_KEY", 
    secretAccessKey: "MY_SECRET_KEY", 
    logger: console 
    }); 

// Setup some service objects 
    var docClient = new AWS.DynamoDB.DocumentClient(); 

    function write_to_db(Wyear,Wsports,Wname){ 
     var year = Wyear; 
     var sports = Wsports; 
     var name = Wname; 
    //setting write parameters 
    var params = { 
     TableName :"table", 
     Item:{ 
      "year": year, 
      "sports": sports, 
      "name": name 
     } 
    }; 


    //writting data 
    docClient.put(params, function(err, data) { 
     if (err) { 
      console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2)); 
     } else { 
      console.log("Added item:", JSON.stringify(data, null, 2)); 
     } 
    }); 
    } 

function read_from_db(Ryear,Rsports){ 

    var year = Ryear; 
    var sports = Rsports; 

    var params = { 
     TableName: "table", 
     Key:{ 
      "year": year 
     } 
    }; 

    docClient.get(params, function(err, data) { 
     if (err) { 
      console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); 
     } else { 
      console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
      document.getElementById("message").innerHTML = "GetItem succeeded: " + "\n" + JSON.stringify(data, null, 2); 
     } 
    }); 

} 

function read_from_db(Ryear,Rsports){ 

    var year = Ryear; 
    var sports = Rsports; 

    var params = { 
     TableName: "table", 
     Key:{ 
      "year": year 
     } 
    }; 

    docClient.get(params, function(err, data) { 
     if (err) { 
      console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); 
     } else { 
      console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
      document.getElementById("message").innerHTML = "GetItem succeeded: " + "\n" + JSON.stringify(data, null, 2); 
     } 
    }); 

} 


})(); 

但是當我把一個又一個JavaScript和

appInfo = { 
    db: { 
    region: 'us-west-2', 
    tableName: 'sports_table', 
    readCredentials: { 
     accessKeyId: 'key', 
     secretAccessKey: 'key' 
    } 
    } 
}; 

並獲得通過

AWS.config.update({ 
    region: appInfo.db.region, 
    credentials: appInfo.db.readCredentials, 
    logger: console 
    }); 

// Setup some service objects 
    var dbReader = new AWS.DynamoDB({params: {TableName: appInfo.db.tableName}}); 

現在是沒有得到連接。任何錯誤?有什麼建議麼 ?

+0

'任何錯誤?有什麼建議嗎?'只有你有權限訪問某些日誌中的任何錯誤 –

回答

0

創建的AWS.Credentials一個實例,並使用它:

var credentials = new AWS.Credentials(
    appInfo.db.readCredentials.accessKeyId, 
    appInfo.db.readCredentials.secretAccessKey, 
    null); 
相關問題