【AWS】API Gateway创建Rest API--从S3下载文件

这篇具有很好参考价值的文章主要介绍了【AWS】API Gateway创建Rest API--从S3下载文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 一、背景

在不给AK,SK的前提下,用户查看s3上文件(从s3下载文件)

二、创建API

1、打开API Gateway,点击创建API,选择REST API

【AWS】API Gateway创建Rest API--从S3下载文件

REST API和HTTP API区别:(来自AWS官网)

REST API 和 HTTP API 都是 RESTful API 产品。REST API 支持的功能比 HTTP API 多,而 HTTP API 在设计时功能就极少,因此能够以更低的价格提供。如果您需要如 API 密钥、每客户端节流、请求验证、AWS WAF 集成或私有 API 端点等功能,请选择 REST API。如果您不需要 REST API 中包含的功能,请选择 HTTP API。

2、 设置API名称,选择终端节点类型

【AWS】API Gateway创建Rest API--从S3下载文件

终端节点类型:(来自AWS官网)

        区域性(REGIONAL):适用于同一区域中的客户端。当在 EC2 实例上运行的客户端调用同一区域中的 API,或 API 用于为具有高需求的少数客户端提供服务时,区域 API 可以降低连接开销。

        还有边缘优化(EDGE):最适合地理位置分散的客户端。API 请求将路由到最近的 CloudFront 接入点 (POP)。这是 API Gateway REST API 的默认终端节点类型。

        私有(PRIVATE):是一个只能使用接口 VPC 终端节点从 Amazon Virtual Private Cloud (VPC) 访问的 API 终端节点,该接口是您在 VPC 中创建的终端节点网络接口 (ENI)

 三、配置API

1、进入刚创建好的API,点击资源页,创建资源及方法

1.1创建资源, / 代表根目录,右击选择创建资源

【AWS】API Gateway创建Rest API--从S3下载文件

【AWS】API Gateway创建Rest API--从S3下载文件

 1.2创建方法,上传文件到s3,所以选择GET方法

【AWS】API Gateway创建Rest API--从S3下载文件

【AWS】API Gateway创建Rest API--从S3下载文件

1.3点击刚创建的方法,进入集成请求

【AWS】API Gateway创建Rest API--从S3下载文件

 1.3配置:

集成类型:AWS 服务

AWS区域:(选择相应的区域)

AWS服务:S3

AWS子域:(不用填)

HTTP方法:GET

操作类型:路径覆盖

路径覆盖(可选):{bucket}/{object}

执行角色:(填写有执行API角色的ARN)

【AWS】API Gateway创建Rest API--从S3下载文件

 路径覆盖也可以把路径某部分hard code

 在下方URL路径参数中填写路径参数映射关系

【AWS】API Gateway创建Rest API--从S3下载文件

2、配置设置

翻到最下面,修改二进制媒体类型为   */*

Content-Encoding可以根据需要修改

默认上传文件大小不超过10M

【AWS】API Gateway创建Rest API--从S3下载文件

三、添加授权方

 1、新建Lambda函数,来验证授权方,运行时选择 Node.js 16.x

代码如下:当header中account和password匹配上,则allow,否则deny

exports.handler = function(event, context, callback) {        
    console.log('Received event:', JSON.stringify(event, null, 2));

    // A simple request-based authorizer example to demonstrate how to use request 
    // parameters to allow or deny a request. In this example, a request is  
    // authorized if the client-supplied headerauth1 header, QueryString1
    // query parameter, and stage variable of StageVar1 all match
    // specified values of 'headerValue1', 'queryValue1', and 'stageValue1',
    // respectively.

    // Retrieve request parameters from the Lambda function input:
    var headers = event.headers;
    var queryStringParameters = event.queryStringParameters;
    var pathParameters = event.pathParameters;
    var stageVariables = event.stageVariables;
        
    // Parse the input for the parameter values
    var tmp = event.methodArn.split(':');
    var apiGatewayArnTmp = tmp[5].split('/');
    var awsAccountId = tmp[4];
    var region = tmp[3];
    var restApiId = apiGatewayArnTmp[0];
    var stage = apiGatewayArnTmp[1];
    var method = apiGatewayArnTmp[2];
    var resource = '/'; // root resource
    if (apiGatewayArnTmp[3]) {
        resource += apiGatewayArnTmp[3];
    }
        
    // Perform authorization to return the Allow policy for correct parameters and 
    // the 'Unauthorized' error, otherwise.
    var authResponse = {};
    var condition = {};
    condition.IpAddress = {};
     
    if (headers.account === ""
        && headers.password === "") {
        callback(null, generateAllow('me', event.methodArn));
    }else {
        callback("Unauthorized");
    }
}
     
// Help function to generate an IAM policy
var generatePolicy = function(principalId, effect, resource) {
    // Required output:
    var authResponse = {};
    authResponse.principalId = principalId;
    if (effect && resource) {
        var policyDocument = {};
        policyDocument.Version = '2012-10-17'; // default version
        policyDocument.Statement = [];
        var statementOne = {};
        statementOne.Action = 'execute-api:Invoke'; // default action
        statementOne.Effect = effect;
        statementOne.Resource = resource;
        policyDocument.Statement[0] = statementOne;
        authResponse.policyDocument = policyDocument;
    }
    // Optional output with custom properties of the String, Number or Boolean type.
    authResponse.context = {
        "account": '',
        "password": '',
        "booleanKey": true
    };
    return authResponse;
}
     
var generateAllow = function(principalId, resource) {
    return generatePolicy(principalId, 'Allow', resource);
}
     
var generateDeny = function(principalId, resource) {
    return generatePolicy(principalId, 'Deny', resource);
}

2、创建授权方

授权方名称

类型:选择Lambda

Lambda函数:填写刚创建好的Lambda函数名称

Lambda调用角色:填写调用Lambda函数的角色

Lambda事件负载:选择请求

身份来源:选择标头,添加account和password

授权缓存:取消启用

【AWS】API Gateway创建Rest API--从S3下载文件

 三、配置授权方

选择 添加授权方的路径资源方法中的方法请求

 

【AWS】API Gateway创建Rest API--从S3下载文件

授权选择配置好的授权方名称

请求验证程序:无

需要API密钥:否

HTTP请求标头:将account和password配置进来

【AWS】API Gateway创建Rest API--从S3下载文件

四、部署API

 API配置完成后,右击根目录,部署API, 选择部署阶段,点击部署

【AWS】API Gateway创建Rest API--从S3下载文件

注意:每次对API进行更改后要重新部署一下

五、测试API 

 测试通过两种方式:①Postman        ②python代码

获取URL链接

【AWS】API Gateway创建Rest API--从S3下载文件

1、Postman

进入Postman,添加PUT请求,复制URL链接,在其后添加要下载文件的S3的路径,点击send,即可在下方看到请求结果

【AWS】API Gateway创建Rest API--从S3下载文件

【AWS】API Gateway创建Rest API--从S3下载文件

 

2、python代码

import json
import requests

def call_get_api(_url,_headers):
    res = requests.get(url=_url, headers=_headers)
    return res

def download_s3(bucket,key,local_file):
    # api gateway call url
    url_ip = ""
    # generate the url
    url = url_ip + bucket + key

    # headers
    headers = {"account": "", "password": ""}

    # call the api2s3 method
    res = call_get_api(url, headers)
    res.encoding = 'utf-8'
    data = res.text
    if res.status_code == 200:
        print(res.status_code)
        print(data)
        with open(local_file, 'wb') as f:
            # str通过encode()方法可以转换为bytes
            f.write(data.encode())
    else:
        print(res)

if __name__ == '__main__':
    # s3 file
    bucket = ''
    key = ''

    # local file name
    local_file = ''
    download_s3(bucket, key, local_file)


六、通过CloudFormation新建API

yaml文件代码如下文章来源地址https://www.toymoban.com/news/detail-512110.html

AWSTemplateFormatVersion: '2010-09-09'
Description : Template to provision ETL Workflow for api gateway 
Parameters:
  Region:
    Description: 'Specify the Region for resource.'
    Type: String
    Default: ase1
  Iteration:
    Type: String
    Description: 'Specify the Iteration for Lambda.'
    Default: '001'
  S3Iteration:
    Type: String
    Description: 'Specify the Iteration for S3'
    Default: '001'
  IAMIteration:
    Type: String
    Description: 'Specify the Iteration for IAM roles.'
    Default: '001'

Resources: 
  ApigatewayRestAPI:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: api-downloads3-${Iteration}
      BinaryMediaTypes:
        - "*/*"
      Description: create api to download file from s3
      Mode: overwrite
      EndpointConfiguration:
        Types:
          - REGIONAL
 
  ApigatewayAuthorizer:
    Type: AWS::ApiGateway::Authorizer
    Properties:
      AuthorizerCredentials: "arn:aws:iam::${AWS::AccountId}:role/iamr-replication-${IAMIteration}"
      AuthorizerResultTtlInSeconds : 0
      AuthorizerUri: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:lamb-apigw-authorizer-${S3Iteration}/invocations"
      Type : REQUEST
      AuthType: custom
      RestApiId: 
        !Ref  ApigatewayRestAPI
      Name: auth-request
      IdentitySource : method.request.header.account,method.request.header.password

  ApigatewayResourceFolder:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: 
        !Ref ApigatewayRestAPI
      PathPart: "{folder}"
      ParentId: !GetAtt 
        - ApigatewayRestAPI
        - RootResourceId

  ApigatewayMethodFolder:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizerId:
        !Ref ApigatewayAuthorizer
      AuthorizationType: custom
      RequestParameters: {
        "method.request.path.folder": true,
        "method.request.header.account": true,
        "method.request.header.password": true
      }
      HttpMethod: GET
      MethodResponses:
        - StatusCode: 200
          ResponseModels: 
            application/json: Empty
      RestApiId:
        !Ref ApigatewayRestAPI
      ResourceId: !GetAtt 
        - ApigatewayResourceFolder
        - ResourceId 
      Integration:
        Type: AWS
        Credentials: "arn:aws:iam::${AWS::AccountId}:role/iamr-replication-${IAMIteration}"
        IntegrationHttpMethod: GET
        IntegrationResponses:
          - StatusCode: 200
        PassthroughBehavior: when_no_match
        Uri: "arn:aws:apigateway:${AWS::Region}:s3:path/{folder}"
        RequestParameters: {
            "integration.request.path.folder" : "method.request.path.folder"
          }

  ApigatewayResourceTablename:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: 
        !Ref ApigatewayRestAPI
      PathPart: "{tablename}"
      ParentId:
        !Ref ApigatewayResourceFolder

  ApigatewayMethodTablename:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizerId:
        !Ref ApigatewayAuthorizer
      AuthorizationType: custom
      RequestParameters: {
        "method.request.path.folder": true,
        "method.request.path.tablename": true,
        "method.request.header.account": true,
        "method.request.header.password": true
      }
      HttpMethod: GET
      MethodResponses:
        - StatusCode: 200
          ResponseModels: 
            application/json: Empty
      RestApiId:
        !Ref ApigatewayRestAPI
      ResourceId: !GetAtt 
        - ApigatewayResourceTablename
        - ResourceId 
      Integration:
        Type: AWS
        Credentials: "arn:aws:iam::${AWS::AccountId}:role/iamr-replication-${IAMIteration}"
        IntegrationHttpMethod: GET
        IntegrationResponses:
          - StatusCode: 200
        PassthroughBehavior: when_no_match
        Uri: "arn:aws:apigateway:${AWS::Region}:s3:path/{folder}/{tablename}"
        RequestParameters: {
            "integration.request.path.folder" : "method.request.path.folder",
            "integration.request.path.tablename" : "method.request.path.tablename"
          }


  ApigatewayResourcePartition:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId:
        !Ref ApigatewayRestAPI
      PathPart: "{partition}"
      ParentId:
        !Ref ApigatewayResourceTablename

  ApigatewayMethodPartition:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizerId:
        !Ref ApigatewayAuthorizer
      AuthorizationType: custom
      RequestParameters: {
        "method.request.path.folder": true,
        "method.request.path.tablename": true,
        "method.request.path.partition": true,
        "method.request.header.account": true,
        "method.request.header.password": true
      }
      HttpMethod: GET
      MethodResponses:
        - StatusCode: 200
          ResponseModels: 
            application/json: Empty
      RestApiId:
        !Ref ApigatewayRestAPI
      ResourceId: !GetAtt 
        - ApigatewayResourcePartition
        - ResourceId 
      Integration:
        Type: AWS
        Credentials: "arn:aws:iam::${AWS::AccountId}:role/iamr-replication-${IAMIteration}"
        IntegrationHttpMethod: GET
        IntegrationResponses:
          - StatusCode: 200
        PassthroughBehavior: when_no_match
        Uri: "arn:aws:apigateway:${AWS::Region}:s3:path/{folder}/{tablename}/{partition}"
        RequestParameters: {
            "integration.request.path.partition" : "method.request.path.partition",
            "integration.request.path.folder" : "method.request.path.folder",
            "integration.request.path.tablename" : "method.request.path.tablename"
          }


  ApigatewayResourceFilename:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: 
        !Ref ApigatewayRestAPI
      PathPart: "{filename}"
      ParentId:
        !Ref ApigatewayResourcePartition

  ApigatewayMethodFilename:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizerId:
        !Ref ApigatewayAuthorizer
      AuthorizationType: custom
      RequestParameters: {
        "method.request.path.folder": true,
        "method.request.path.tablename": true,
        "method.request.path.partition": true,
        "method.request.path.filename": true,
        "method.request.header.account": true,
        "method.request.header.password": true
      }
      HttpMethod: GET
      MethodResponses:
        - StatusCode: 200
          ResponseModels: 
            application/json: Empty
      RestApiId:
        !Ref ApigatewayRestAPI
      ResourceId: !GetAtt 
        - ApigatewayResourceFilename
        - ResourceId 
      Integration:
        Type: AWS
        Credentials: "arn:aws:iam::${AWS::AccountId}:role/iamr-replication-${IAMIteration}"
        IntegrationHttpMethod: GET
        IntegrationResponses:
          - StatusCode: 200
        PassthroughBehavior: when_no_match
        Uri: "arn:aws:apigateway:${AWS::Region}:s3:path/{folder}/{tablename}/{partition}/{filename}"
        RequestParameters: {
            "integration.request.path.partition" : "method.request.path.partition",
            "integration.request.path.filename" : "method.request.path.filename",
            "integration.request.path.folder" : "method.request.path.folder",
            "integration.request.path.tablename" : "method.request.path.tablename"
          }
          
  ApigatewayDeploymentv1:
    DependsOn: ApigatewayMethodFilename
    Type: AWS::ApiGateway::Deployment
    Properties: 
      RestApiId:
        !Ref ApigatewayRestAPI
      StageName : v1

  PermissionToInvokeLambda: 
    Type: AWS::Lambda::Permission
    Properties: 
      FunctionName: lamb-apigw-authorizer-${Iteration}
      Action: "lambda:InvokeFunction"
      Principal: "apigateway.amazonaws.com"
      SourceArn: !Sub
        - "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${APiId}/authorizers/${AuthorizerId}"
        - APiId: 
            !Ref ApigatewayRestAPI
          AuthorizerId: 
            !Ref ApigatewayAuthorizer

Outputs:
  RootResourceId:
    Value: !GetAtt ApigatewayRestAPI.RootResourceId
  AuthorizerId:
    Value: !GetAtt ApigatewayAuthorizer.AuthorizerId

到了这里,关于【AWS】API Gateway创建Rest API--从S3下载文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python文件上传 S3(AWS) 简单实现

    建立aws账户,进入到S3界面  点击 \\\"Create bucket\\\" 一系列操作之后——这里给bucket命名为csfyp python需要先: 这两个包含一些连接python和s3 连接的api 然后直接上代码

    2024年02月03日
    浏览(39)
  • 【Terraform学习】使用 Terraform创建 S3 存储桶事件(Terraform-AWS最佳实战学习)

      本站以分享各种运维经验和运维所需要的技能为主 《python》:python零基础入门学习 《shell》:shell学习 《terraform》持续更新中:terraform_Aws学习零基础入门到最佳实战 《k8》暂未更新 《docker学习》暂未更新 《ceph学习》ceph日常问题解决分享 《日志收集》ELK+各种中间件 《运

    2024年02月10日
    浏览(31)
  • 使用AWS MVP方案[Data Transfer Hub]从Global S3同步文件到中国区S3

    本文主要描述在AWS Global区部署Data Transfer Hub方案,并创建从global S3同步文件到中国区S3的任务   1.1 AWS Global账号 需要一个AWS Global的账号,并且有相应的权限,本例是Full Administrator权限 1.2 在AWS Global账号下准备一个S3存储桶 登陆AWS Global账号,选择 服务 - 存储 - S3   点击创建

    2024年02月08日
    浏览(36)
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹

    本文收录于【#云计算入门与实践 - AWS】专栏中,收录 AWS 入门与实践相关博文。 本文同步于个人公众号:【 云计算洞察 】 更多关于云计算技术内容敬请关注:CSDN【#云计算入门与实践 - AWS】专栏。 本系列已更新博文: [ 云计算 | AWS 实践 ] Java 应用中使用 Amazon S3 进行存储桶

    2024年02月08日
    浏览(47)
  • Jira REST API_获取创建issue时的字段配置

    通过 Jira REST API 创建 jira issue 时,可以根据jira 配置,动态获取需要填写的字段;这样就不用每次通过UI ,固定指定创建issue时需要填充的内容,来实现接口创建 issue 了。 获取创建项目的问题类型: 获取指定问题类型创建时的字段配置 可以先获取项目类型id,然后再在末尾传

    2024年01月17日
    浏览(33)
  • 使用 API Gateway Integrator 在 Quarkus 中实施适用于 AWS Lambda 的 OpenAPI

    AWS API Gateway 集成使得使用符合 OpenAPI 标准的 Lambda Function 轻松实现 REST API。         它是一个   允许以标准方式描述 REST API 的规范。   OpenAPI规范 (OAS) 为 REST API 定义了与编程语言无关的标准接口描述。这使得人类和计算机都可以发现和理解服务的功能,而无需访问源代

    2024年02月13日
    浏览(41)
  • 如何使用Python Flask和MySQL创建管理用户的REST API

    部分数据来源: ChatGPT  引言         在现代化的应用开发中,数据库是一个非常重要的组成部分。关系型数据库(例如:MySQL、PostgreSQL)在这方面尤其是很流行。Flask是一个Python的web框架,非常适合实现REST API。在这篇文章中,我们将介绍如何使用Python Flask和MySQL创建一个

    2024年02月08日
    浏览(53)
  • Azure Machine Learning - 使用 REST API 创建 Azure AI 搜索索引

    本文介绍如何使用 Azure AI 搜索 REST AP和用于发送和接收请求的 REST 客户端以交互方式构建请求。 关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资深架构师,项目管理

    2024年02月04日
    浏览(54)
  • 使用Java API对HDFS进行如下操作:文件的创建、上传、下载以及删除等操作

    HDFS-JAVA接口:上传文件 将一个本地文件(无具体要求)上传至HDFS中的/hdfs-test路径下(如无此路径,新建一个)。 新建路径:   首先在路径/usr/test/ 下新建test.txt,指令为:/usr/test/test.txt,然后进行上传操作。      2.HDFS-JAVA接口:创建文件 在HDFS中的/hdfs-test路径下新建一个da

    2024年02月07日
    浏览(41)
  • AWS复制EC2文件到S3,g4dn.2xlarge没有NVIDIA GPU 驱动问题

    1、给instances权限 action Security modify IAM role 把提前创建好的role给这个instance即可 2、复制到bucket 如果要自己安装,参考https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-nvidia-driver.html#public-nvidia-driver 但实际上只需要选择操作系统的时候选择带有GPU的就可以了,docker和python3也自动就

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包