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、这是跨平台的拼十辅助器黑科...
实测辅助!胡乐辅助脚本的正确使... 【亲,胡乐辅助脚本的正确使用方法视频 这款游戏可以开挂的,确实是有挂的,很多玩家在这款胡乐辅助脚本的...
2分钟辅助!新道游正版辅助,微... 【福星临门,好运相随】;2分钟辅助!新道游正版辅助,微乐兰州麻将小程序辅助(研究成果开挂辅助插件);...
通报辅助!吉林心悦游戏辅助,战... 通报辅助!吉林心悦游戏辅助,战皇大厅辅助那个可靠,玩家科普开挂(有挂存在) >>您好:软件加薇136...
九分钟辅助!xpoker辅助器... 九分钟辅助!xpoker辅助器,创思维激k必胜辅助(查到实测辅助开挂辅助安装);亲真的是有正版授权,...
实测辅助!河南微乐小程序辅助器... 实测辅助!河南微乐小程序辅助器免费,天天福建辅助器,透视最新开挂(有挂规律) 【无需打开直接搜索加薇...
3分钟辅助!wepoker辅助... 3分钟辅助!wepoker辅助器最新版本更新内容,人海大厅软件辅助(玩家科普开挂辅助插件);一、人海...
教会辅助!广东雀神智能插件辅助... 广东雀神智能插件辅助脚本 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理...
第5分钟辅助!wepoker怎... 第5分钟辅助!wepoker怎么设置盖牌,手游蛮王辅助(一分钟了解开挂辅助平台);AI辅助机器人普及...
关于辅助!丽水都来辅助器,情怀... 关于辅助!丽水都来辅助器,情怀宜春怎么开挂,透视存在开挂(有挂存在)1、下载安装好情怀宜春怎么开挂,...