【问题标题】:Cannot call AWS StepFunctions in a browser with AWS javascript SDK because of CORS由于 CORS,无法使用 AWS javascript SDK 在浏览器中调用 AWS StepFunctions
【发布时间】:2019-04-02 23:38:30
【问题描述】:

我正在尝试在浏览器中使用 AWS StepFunctions API(角度,使用 aws javascript API)。 使用此代码:

import * as StepFunctions from "aws-sdk/clients/stepfunctions";

   let sf = new StepFunctions({apiVersion: '2016-11-23'});
   var request: GetExecutionHistoryInput = {
       executionArn: executionArn,
       maxResults: 1000,
       reverseOrder: false
   }

   sf.getExecutionHistory(request).promise()

浏览器出现错误:

 OPTIONS https://states.eu-west-1.amazonaws.com/ 404 (Not Found)
 Failed to load https://states.eu-west-1.amazonaws.com/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

这是否意味着 AWS StepFunctions 还没有准备好在浏览器端使用? 如果是真的,它在 AWS 文档中的什么地方记录?

【问题讨论】:

    标签: javascript amazon-web-services cors aws-step-functions aws-sdk-js


    【解决方案1】:

    在尝试区分标准 Javascript SDK 和用于浏览器的 Javascript SDK 时,文档很难理解。

    正如问题所推测的那样,StepFunctions 实际上无法通过浏览器使用。

    这是github问题评论中的官方声明:

    AWS Step Functions 服务不支持通过 CORS 进行访问 请求,因此无法从浏览器中使用。

    https://github.com/aws/aws-sdk-js/issues/1334#issuecomment-276215833

    也许如果浏览器 SDK 没有直接链接到标准 API 参考,就不会有那么多混乱。

    【讨论】:

      【解决方案2】:

      2021 年 5 月向 AWS Step Functions 添加了 CORS 支持。 所以现在可以使用 @aws-sdk/client-sfn 了,这是一个非常基本的 TypeScript 代码:

      import { SFNClient, DescribeStateMachineForExecutionCommand } from '@aws-sdk/client-sfn';
      const region = 'us-east-1';
      const config = {
        region,
        credentials: {
          accessKeyId: 'xxxx', // your temporary cred,
          secretAccessKey: 'xxxxx', // your temporary cred,
        },
      };
      const client = new SFNClient(config);
      async function getDataByExecutionArn(arn: string, onChunkRead: any) {
        const command = new DescribeStateMachineForExecutionCommand({ executionArn: arn });
        return await client.send(command);
      }
      export default getDataByExecutionArn;
      

      请确保您没有将这些凭据放入在浏览器中运行的 Javascript 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-01
        • 2018-06-27
        • 2019-03-13
        • 2021-08-06
        • 2017-04-29
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        相关资源
        最近更新 更多