【AWS】APIGEY+LAMBDA查找檔案
需求:
前端需要有一個api,能夠帶入參數{檔案名稱},
由lambda去檢查aws s3上是否有該檔案,在respon json格式告知前端
大致流程如下:
(1) 前端Html放在Aws的S3上,透過Ajax對Api getway進行呼叫。
(2) APIGetway接收到後,將queryString url傳遞至Lambda進行處理
(3) Lambda收取到url參數後,透過S3的getObject或是getObjectHeader來進行檔案存在判斷
(4) Lambda將結果respon為json格式,讓前端進行解析。
實作流程如下:
(1) 設置Lambda
透過lambda去解析apiGetway傳遞過來的queryString,並將條件帶入s3.getObjectHeader去查找檔案,在callback記得帶有crossdomain的header。
(下列queryString的寫法是測試,想透過非正常管道獲取參數,建議走正規透過event獲取)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const S3 = new AWS.S3({ | |
signatureVersion: 'v4', | |
}); | |
console.log("start..."); | |
exports.handler = (event, context, callback) => | |
{ | |
var paramFileName = "";//video20180805 | |
if (event.queryStringParameters !== null && event.queryStringParameters !== undefined) { | |
if (event.queryStringParameters.url !== undefined && | |
event.queryStringParameters.url !== null && | |
event.queryStringParameters.url !== "") { | |
console.log("Received url: " + event.queryStringParameters.url); | |
paramFileName = event.queryStringParameters.url; | |
} | |
} | |
//取得環境參數 | |
const BUCKET = process.env['BUCKET']; | |
const FILE = "data/"+paramFileName+".mp4"; //ex:data/video20180805.mp4"; | |
const params = { | |
Bucket: BUCKET, | |
Key: FILE, | |
}; | |
console.log("Bucket:"+BUCKET); | |
console.log("FILE:"+FILE); | |
S3.headObject(params, function (err, metadata) { | |
if (err && err.code === 'NotFound') { | |
//console.log("======XXXX========"); | |
var data = {message:"not find",urlParam:paramFileName,hasFile:false}; | |
callback(null, {statusCode: 200,headers: {"Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials" : true },body: JSON.stringify(data) }); | |
} else { | |
//console.log("======OOOO========"); | |
var data = {message:"find",urlParam:paramFileName,hasFile:true}; | |
callback(null, {statusCode: 200,headers: {"Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials" : true },body: JSON.stringify(data) }); | |
} | |
}); | |
}; |
(2) 設置Apigetway
這裡不用特地設置參數url,
是因為lambda解析是直接去解析apigetway的queryString
上述方式並不是正規方式,只為了試試看不正規的方式是否能成功所使用
(建議用正規的方式,apigetway設置參數,用Lambda 透過event來進行擷取參數)
這邊要設置lambda的名稱,以及開啟cos設定(避免corssdomain)。
最後記得要depoly,apigetway才會生效。
(3) Ajax呼叫apigetway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var finalVideoJsonPath = "http://apigetwayPath?url={check}" | |
function CheckFileJson() | |
{ | |
console.log("----------------------CheckFileJson---------------------"); | |
$.ajax({ | |
type: 'GET', | |
url: finalVideoJsonPath, | |
dataType: 'json', | |
crossDomain: true, | |
success: function(data) | |
{ | |
console.log("[success]LoadVideoJson ,video:"+data["video"]+" ,hasFile:"+data["hasFile"]); | |
if(data["hasFile"]==true) | |
{ | |
//has File | |
} | |
else | |
{ | |
//not found File | |
} | |
} | |
}).fail(function() { | |
//not found File | |
}); | |
} |
如果有相關crossdomain問題也可以參照之前的文章
Aws Crossdomain解決
張貼留言