一种AWS API网关资源的两种HTTP方法

时间:2019-07-29 23:11:45

标签: amazon-cloudformation aws-api-gateway

关于通过CloudFormation为CORS配置API网关方法的问题,post颇为可恶,我正在努力。我想用两个方法“ options”和“ post”创建以下端点:

/image/submit

这是我的CF模板代码段:

ApiDefault:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Name: "Stash-Default"
      FailOnWarnings: true
  ApiDefaultDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn:
      - "ApiMethodImageSubmitPost"
      - "ApiMethodImageSubmitOption"
    Properties:
      RestApiId: !Ref "ApiDefault"
      StageName: "v1"
  ApiResourceImage:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      ParentId: !GetAtt ["ApiDefault", "RootResourceId"]
      PathPart: "image"
      RestApiId: !Ref "ApiDefault"
  ApiResourceImageSubmit:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      ParentId: !Ref "ApiResourceImage"
      PathPart: "submit"
      RestApiId: !Ref "ApiDefault"
  ApiMethodImageSubmitPost:
    Type: "AWS::ApiGateway::Method"
    Properties:
      HttpMethod: "POST"
      AuthorizationType: "NONE"
      MethodResponses:
        - StatusCode: "200"
      Integration:
        IntegrationHttpMethod: "POST"
        Type: "AWS_PROXY"
        IntegrationResponses:
          - StatusCode: "200"
        Credentials: !GetAtt [ "ExecuteApiMethodImageSubmit", "Arn" ]
        Uri: !Sub
          - "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
          - lambdaArn: !GetAtt [ "ImageReceive", "Arn" ]
      RestApiId: !Ref "ApiDefault"
      ResourceId: !Ref "ApiResourceImageSubmit"
  ApiMethodImageSubmitOption:
    Type: "AWS::ApiGateway::Method"
    Properties:
      HttpMethod: "OPTIONS"
      AuthorizationType: "NONE"
      Integration:
        Type: "MOCK"
        IntegrationResponses:
          - StatusCode: "200"
            ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
      MethodResponses:
        - StatusCode: "200"
          ResponseModels:
            application/json: "Empty"
          ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: false
            method.response.header.Access-Control-Allow-Methods: false
            method.response.header.Access-Control-Allow-Origin: false
      RestApiId: !Ref "ApiDefault"
      ResourceId: !Ref "ApiResourceImageSubmit"

炸弹说 ApiMethodImageSubmitPost

  

该资源的方法已经存在(服务:AmazonApiGateway;   状态码:409;错误代码:ConflictException;要求编号:   454cf46a-b434-4626-bd4b-b6d4fe21142c)

您能以这种方式为单个API资源创建两个http方法吗?我对这方面的AWS文档没有很多运气。

0 个答案:

没有答案