AWS中的Terraform跨域问题:s3+CFApiGateway
创始人
2024-09-28 03:01:03
0

在AWS中使用Terraform创建s3存储桶和CloudFront分发,以及一个API网关。当在前端网页上通过API网关访问存储桶中的对象时,会遇到跨域问题。

解决此问题的方法是在API网关中添加CORS配置:

resource "aws_api_gateway_rest_api" "example" { name = "example" }

resource "aws_api_gateway_resource" "example" { rest_api_id = aws_api_gateway_rest_api.example.id parent_id = aws_api_gateway_rest_api.example.root_resource_id path_part = "example" }

resource "aws_api_gateway_method" "get" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = "GET" authorization = "NONE" }

resource "aws_api_gateway_integration" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = aws_api_gateway_method.get.http_method

type = "AWS_PROXY"

integration_http_method = "GET" uri = "arn:aws:apigateway:${var.region}:s3:path/${var.bucket}/${var.path}" }

resource "aws_api_gateway_method_response" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = aws_api_gateway_method.get.http_method status_code = "200"

response_parameters = { "method.response.header.Access-Control-Allow-Origin" = true } }

resource "aws_api_gateway_integration_response" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = aws_api_gateway_method.get.http_method status_code = "200"

response_templates = { "application/json" = "" }

response_parameters = { "method.response.header.Access-Control-Allow-Origin" = "'*'" } }

resource "aws_api_gateway_deployment" "example" { depends_on = [aws_api_gateway_integration_response.example] rest_api_id = aws_api_gateway_rest_api.example.id stage_name = "dev" }

此代码示例中,通过AWS API网关和S3集成,从而可以解决跨域问题。对于需要访问存储桶中数据的前端应用,可以通过此方式轻松实现。

相关内容

热门资讯

微扑克辅助软件!德扑之星内部(... 1、微扑克辅助软件!德扑之星内部(透视)好像存在有挂(详细辅助详细教程);代表性(透视辅助软件透明挂...
透视神器!WePoKe外 挂,... 透视神器!WePoKe外 挂,wepower有机器人吗,详细透视德州论坛在进入WePoKe外 挂辅助...
wpk发牌逻辑!pokerwo... wpk发牌逻辑!pokerworld有挂吗(透视)起初真的是有挂(详细辅助安装教程)1、wpk发牌逻...
透视透视!wepoke有挂,w... 透视透视!wepoke有挂,wopoker轻量版外挂,详细透视技巧教程1、下载好wepoke有挂辅助...
wepoke辅助德之星!wpk... wepoke辅助德之星!wpk胜率跟号有关系么(透视)原先存在有挂(详细辅助总结教程)您好,wepo...
透视新版!wepoke辅助技巧... 透视新版!wepoke辅助技巧,wopoker辅助真的假的,详细透视透明教程1.wepoke辅助技巧...
aapoker俱乐部!微扑克发... aapoker俱乐部!微扑克发牌系统(透视)素来真的有挂(详细辅助线上教程);aapoker俱乐部!...
透视代打!wepower辅助器... 透视代打!wepower辅助器,wepoke ai,详细透视AA德州教程1、上手简单,内置详细流程视...
德扑ai智能!扑克时间辅助(透... 德扑ai智能!扑克时间辅助(透视)其实是有挂(详细辅助曝光教程)是一款可以让一直输的玩家,快速成为一...
透视脚本!wepoke有辅助挂... 透视脚本!wepoke有辅助挂,wepoke软件透明挂存在吗,详细透视透明挂教程1、点击下载安装,w...