{"id":9997,"date":"2023-03-20T17:25:29","date_gmt":"2023-03-20T09:25:29","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9997"},"modified":"2023-03-20T17:25:29","modified_gmt":"2023-03-20T09:25:29","slug":"api-gateway%e5%88%9b%e5%bb%barest-api-%e5%b0%86%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%88%b0s3%ef%bc%88%e8%bf%9b%e9%98%b6%e7%89%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9997","title":{"rendered":"API Gateway\u521b\u5efaRest API&#8211;\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230S3\uff08\u8fdb\u9636\u7248\uff09"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u80cc\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u662f\u57fa\u7840\u7248\uff0c\u8fd9\u7bc7\u6587\u7ae0\u589e\u52a0\u6388\u6743\u65b9<\/p>\n\n\n\n<p><blockquote class=\"wp-embedded-content\" data-secret=\"HRbzCG7lwn\"><a href=\"http:\/\/123.57.164.21\/?p=9981\">API Gateway\u521b\u5efaRest API&#8211;\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230S3\uff08\u57fa\u7840\u7248\uff09<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300aAPI Gateway\u521b\u5efaRest API&#8211;\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230S3\uff08\u57fa\u7840\u7248\uff09\u300b\u201492IT \ud83c\udf6d\" src=\"http:\/\/123.57.164.21\/?p=9981&#038;embed=true#?secret=HRbzCG7lwn\" data-secret=\"HRbzCG7lwn\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n\n\n\n<p><strong>\u00a0\u4e8c\u3001\u521b\u5efa\u6388\u6743\u65b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1\u3001\u65b0\u5efa<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Lambda%E5%87%BD%E6%95%B0&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Lambda\u51fd\u6570<\/a>\uff0c\u6765\u9a8c\u8bc1\u6388\u6743\u65b9\uff0c\u8fd0\u884c\u65f6\u9009\u62e9 Node.js 16.x<\/strong><\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a\u5f53header\u4e2daccount\u548cpassword\u5339\u914d\u4e0a\uff0c\u5219allow\uff0c\u5426\u5219deny<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">exports.handler = function(event, context, callback) {        \n    console.log('Received event:', JSON.stringify(event, null, 2));\n \n    \/\/ A simple request-based authorizer example to demonstrate how to use request \n    \/\/ parameters to allow or deny a request. In this example, a request is  \n    \/\/ authorized if the client-supplied headerauth1 header, QueryString1\n    \/\/ query parameter, and stage variable of StageVar1 all match\n    \/\/ specified values of 'headerValue1', 'queryValue1', and 'stageValue1',\n    \/\/ respectively.\n \n    \/\/ Retrieve request parameters from the Lambda function input:\n    var headers = event.headers;\n    var queryStringParameters = event.queryStringParameters;\n    var pathParameters = event.pathParameters;\n    var stageVariables = event.stageVariables;\n        \n    \/\/ Parse the input for the parameter values\n    var tmp = event.methodArn.split(':');\n    var apiGatewayArnTmp = tmp[5].split('\/');\n    var awsAccountId = tmp[4];\n    var region = tmp[3];\n    var restApiId = apiGatewayArnTmp[0];\n    var stage = apiGatewayArnTmp[1];\n    var method = apiGatewayArnTmp[2];\n    var resource = '\/'; \/\/ root resource\n    if (apiGatewayArnTmp[3]) {\n        resource += apiGatewayArnTmp[3];\n    }\n        \n    \/\/ Perform authorization to return the Allow policy for correct parameters and \n    \/\/ the 'Unauthorized' error, otherwise.\n    var authResponse = {};\n    var condition = {};\n    condition.IpAddress = {};\n     \n    if (headers.account === \"\"\n        &amp;&amp; headers.password === \"\") {\n        callback(null, generateAllow('me', event.methodArn));\n    }else {\n        callback(\"Unauthorized\");\n    }\n}\n     \n\/\/ Help function to generate an IAM policy\nvar generatePolicy = function(principalId, effect, resource) {\n    \/\/ Required output:\n    var authResponse = {};\n    authResponse.principalId = principalId;\n    if (effect &amp;&amp; resource) {\n        var policyDocument = {};\n        policyDocument.Version = '2012-10-17'; \/\/ default version\n        policyDocument.Statement = [];\n        var statementOne = {};\n        statementOne.Action = 'execute-api:Invoke'; \/\/ default action\n        statementOne.Effect = effect;\n        statementOne.Resource = resource;\n        policyDocument.Statement[0] = statementOne;\n        authResponse.policyDocument = policyDocument;\n    }\n    \/\/ Optional output with custom properties of the String, Number or Boolean type.\n    authResponse.context = {\n        \"account\": '',\n        \"password\": '',\n        \"booleanKey\": true\n    };\n    return authResponse;\n}\n     \nvar generateAllow = function(principalId, resource) {\n    return generatePolicy(principalId, 'Allow', resource);\n}\n     \nvar generateDeny = function(principalId, resource) {\n    return generatePolicy(principalId, 'Deny', resource);\n}\n<\/pre>\n\n\n\n<p><strong>2\u3001\u521b\u5efa\u6388\u6743\u65b9<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u6388\u6743\u65b9\u540d\u79f0\n\n\u7c7b\u578b\uff1a\u9009\u62e9Lambda\n\nLambda\u51fd\u6570\uff1a\u586b\u5199\u521a\u521b\u5efa\u597d\u7684Lambda\u51fd\u6570\u540d\u79f0\n\nLambda\u8c03\u7528\u89d2\u8272\uff1a\u586b\u5199\u8c03\u7528Lambda\u51fd\u6570\u7684\u89d2\u8272\n\nLambda\u4e8b\u4ef6\u8d1f\u8f7d\uff1a\u9009\u62e9\u8bf7\u6c42\n\n\u8eab\u4efd\u6765\u6e90\uff1a\u9009\u62e9\u6807\u5934\uff0c\u6dfb\u52a0account\u548cpassword\n\n\u6388\u6743\u7f13\u5b58\uff1a\u53d6\u6d88\u542f\u7528<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-153-947x1024.png\" alt=\"\" class=\"wp-image-9998\" width=\"428\" height=\"462\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-947x1024.png 947w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-277x300.png 277w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-768x831.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-830x898.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-230x249.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-350x379.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153-480x519.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-153.png 1250w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e09\u3001\u914d\u7f6e\u6388\u6743\u65b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9009\u62e9 \u6dfb\u52a0\u6388\u6743\u65b9\u7684\u8def\u5f84\u8d44\u6e90\u65b9\u6cd5\u4e2d\u7684\u65b9\u6cd5\u8bf7\u6c42<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-154-1024x433.png\" alt=\"\" class=\"wp-image-9999\" width=\"488\" height=\"206\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-1024x433.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-300x127.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-768x325.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-1536x649.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-830x351.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-350x148.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154-480x203.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-154.png 1552w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u6388\u6743\u9009\u62e9\u914d\u7f6e\u597d\u7684\u6388\u6743\u65b9\u540d\u79f0\n\n\u8bf7\u6c42\u9a8c\u8bc1\u7a0b\u5e8f:\u65e0\n\n\u9700\u8981API\u5bc6\u94a5:\u5426\n\nHTTP\u8bf7\u6c42\u6807\u5934:\u5c06account\u548cpassword\u914d\u7f6e\u8fdb\u6765<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-155-1024x482.png\" alt=\"\" class=\"wp-image-10000\" width=\"564\" height=\"265\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-1024x482.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-300x141.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-768x361.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-1536x722.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-830x390.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-230x108.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-350x165.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155-480x226.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-155.png 1548w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e09\u3001\u6d4b\u8bd5API\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6d4b\u8bd5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff1a\u2460<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Postman&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Postman<\/a>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2461python\u4ee3\u7801<\/p>\n\n\n\n<p>\u83b7\u53d6URL\u94fe\u63a5<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-156-1024x439.png\" alt=\"\" class=\"wp-image-10001\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-1024x439.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-300x129.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-768x329.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-1536x659.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-830x356.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-230x99.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-350x150.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156-480x206.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-156.png 1544w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>1\u3001Postman<\/strong><\/p>\n\n\n\n<p>\u8fdb\u5165Postman\uff0c\u6dfb\u52a0PUT\u8bf7\u6c42\uff0c\u590d\u5236URL\u94fe\u63a5\uff0c\u5728\u5176\u540e\u6dfb\u52a0\u4e0a\u4f20\u5230S3\u7684\u8def\u5f84\uff0c\u5728headers\u4e2d\u6dfb\u52a0account\u548cpassword\uff0c\u70b9\u51fbsend\uff0c\u5373\u53ef\u5728\u4e0b\u65b9\u770b\u5230\u8bf7\u6c42\u7ed3\u679c<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-157-1024x339.png\" alt=\"\" class=\"wp-image-10002\" width=\"511\" height=\"169\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-1024x339.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-300x99.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-768x254.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-830x275.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-230x76.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-350x116.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157-480x159.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-157.png 1534w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2\u3001python\u4ee3\u7801\uff1a\u5728headers\u4e2d\u6dfb\u52a0account\u548cpassword<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import requests\n \n \ndef call_api(_url, _headers, _files):\n    res = requests.put(_url, data=_files, headers=_headers)\n    return res\n \ndef unload_s3(local_file_name,S3_file_name):\n    # api gateway call url\n    url_ip = \"\"\n    # generate the url\n    url = url_ip + S3_file_name\n \n    # the file on your computer\n    uploadFile = open(local_file_name, \"rb\")\n \n    # request headers\n    headers = {\"account\": \"\",\n               \"password\": \"\"}\n \n    # call the api2s3 method\n    res = call_api(url, headers, uploadFile)\n    # print the result\n    if res.ok:\n        return (\"Upload completed successfully!\")\n    else:\n        return (\"Upload failed!\")\n \nif __name__ == '__main__':\n \n    # local file name \n    local_file_name = ''\n    # S3 file_name to upload\n    S3_file_name = ''\n    unload_s3(local_file_name,S3_file_name)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u80cc\u666f \u4e0a\u4e00\u7bc7\u6587\u7ae0\u662f\u57fa\u7840\u7248\uff0c\u8fd9\u7bc7\u6587\u7ae0\u589e\u52a0\u6388\u6743\u65b9 API Gateway\u521b\u5efaRest API&#8211;\u5c06\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9997"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9997"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9997\/revisions"}],"predecessor-version":[{"id":10003,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9997\/revisions\/10003"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}