Javascript 如何使用Aws sdk 2调用api?

Javascript 如何使用Aws sdk 2调用api?,javascript,angular,api,promise,aws-sdk,Javascript,Angular,Api,Promise,Aws Sdk,我已经使用AWS创建了请求,并希望调用我的AWS SDk API。 下面是我的全部代码 var endpoint = new AWS.Endpoint(AWS.config.domain); var request = new AWS.HttpRequest(endpoint, AWS.config.region); AWS.config.credentials = new AWS.Credentials('somevalue', 'somevalue', ''); var signer =

我已经使用AWS创建了请求,并希望调用我的AWS SDk API。 下面是我的全部代码

var endpoint = new AWS.Endpoint(AWS.config.domain);
var request = new AWS.HttpRequest(endpoint, AWS.config.region);
 AWS.config.credentials = new AWS.Credentials('somevalue', 'somevalue', '');
var signer = new AWS.Signers.V4(request, 'es',true);
signer.addAuthorization(AWS.config.credentials, new Date());
request.body = JSON.stringify({
            'query': {
                my value
        });
现在我已经准备好了对象请求,我需要调用我的API。但是我不知道我需要使用哪个AWS或http方法来调用。我尝试了以下选项,但没有成功。我也使用了request.on方法,但它说它不受支持

`var promise = request.promise();
    promise.then(
        function(body) {
          console.log("yes")
        },
        function(error) {
            console.log("no")
        }
    );
我也使用了下面的代码来调用,但它失败并给出错误

    var client = new AWS.HttpClient();
        client.handleRequest(request,null,function(response) {
            console.log(response.statusCode + ' ' + response.statusMessage);
            var responseBody = '';
            response.on('data', function (chunk) {
              responseBody += chunk;
            });
            response.on('end', function (chunk) {
              console.log('Response body: ' + responseBody);
            });
          }, function(error) {
            console.log('Error: ' + error);
          });

core.js:1448 ERROR TypeError: Cannot read property 'xhrAsync' of null
    at features.constructor.handleRequest (xhr.js:63)

您提供的代码片段只是基于承诺的语法的一个示例。要了解更多关于aws sdk的信息,我建议阅读他们的文档

关于aws sdk使用的另一个答案可以在此处找到:

AWS在这里提供了关于其软件包的文档:

下面是一个很好的用法示例:

…显示相关代码段:

//---- Angular 2 Http Service Example ----
import {Injectable} from "@angular/core";
import {Http, Headers} from "@angular/http";
const AWS = require("aws-sdk");

@Injectable()
export class ApiGatewayService {

 constructor(private http:Http){

let creds = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: "eu-west-1:xxx-xxxx-xxxxxxxx"
});

AWS.config.update({
  region: 'eu-west-1',
  credentials: creds
});

AWS.config.credentials.get((err) => {
  if(err) throw err;

  let httpRequest = new AWS.HttpRequest("https://xxxxx.execute-api.eu-west-1.amazonaws.com/v1/my/api", "eu-west-1");
  httpRequest.method = "POST";
  httpRequest.headers.host = "xxxxx.execute-api.eu-west-1.amazonaws.com";
  httpRequest.headers['Content-Type'] = "application/json";
  httpRequest.body = JSON.stringify({
    data: {
      username: 'foo',
      password: 'bar',
      applicationName: 'biz',
    }
  });

  let v4signer = new AWS.Signers.V4(httpRequest, "execute-api", true);
  v4signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());

  //Create a headers object from the AWS signed request
  let headers = new Headers();
  for(let key of Object.keys(httpRequest.headers)){
    headers.set(key, httpRequest.headers[key]);
  }
  headers.delete('host'); //Browser throws a warning if attempting to set host header
  this.http.post(httpRequest.endpoint.href, httpRequest.body, {
    method: httpRequest.method,
    search: httpRequest.endpoint.search,
    headers: headers
  }).subscribe((data)=>{
    console.log(data); //Success
  });
})
 }
}

@凯尔,谢谢你的回复,我已经编辑了我的问题,您能否提供更多详细信息。我浏览了您提供的链接,但内容不多。扩展了我的答案,提供了一个指向要点的链接,使用这部分aws SDK感谢您的精彩回复。我做了部分工作。中的访问控制允许标头不允许获取错误请求标头字段X-Amz-User-Agent飞行前回复。你能提供一些细节如何解决这个问题吗?这是一个与你原来的帖子不同的问题,但你可以在这里找到答案:如果我的帖子解决了你最初的问题,我建议你投票并接受作为答案。