Last active
September 29, 2021 16:28
-
-
Save Dylan0916/6b0560874e09ba6ff99e153ab59b5097 to your computer and use it in GitHub Desktop.
添加 get 與 put S3 的 code
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 axios = require("axios"); | |
+ const aws = require("aws-sdk"); | |
+ const s3 = new aws.S3(); | |
+ const bucketName = "test-firebase-dynamic-links"; | |
+ const s3FileName = "links_map.json"; | |
+ function getDataFromS3() { | |
+ const params = { | |
+ Bucket: bucketName, | |
+ Key: s3FileName, | |
+ }; | |
+ | |
+ return s3 | |
+ .getObject(params) | |
+ .promise() | |
+ .then((resp) => resp.Body.toString("utf-8")) | |
+ .then((stringData) => JSON.parse(stringData)); | |
+ } | |
+ function putDataToS3(data) { | |
+ const params = { | |
+ Bucket: bucketName, | |
+ Key: s3FileName, | |
+ Body: JSON.stringify(data), | |
+ ContentType: "application/json", | |
+ }; | |
+ | |
+ return s3.putObject(params).promise(); | |
+ } | |
const headers = { | |
"Access-Control-Allow-Headers": "Content-Type", | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Methods": "OPTIONS,POST,GET", | |
} | |
exports.handler = async (event) => { | |
const queryString = event.queryStringParameters || {}; | |
const { link } = queryString; | |
+ const s3Data = await getDataFromS3(); | |
+ if (link in s3Data) { | |
+ const response = { | |
+ statusCode: 200, | |
+ headers, | |
+ body: JSON.stringify({ link: s3Data[link] }), | |
+ }; | |
+ | |
+ return response; | |
+ } | |
const POST_URL = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=your_key"; | |
const config = { | |
dynamicLinkInfo: { | |
domainUriPrefix: "https://xxx.page.link", | |
link, | |
}, | |
}; | |
const axiosResp = await axios.post(POST_URL, config); | |
const dynamicLink = axiosResp.data.shortLink; | |
+ await putDataToS3({ ...s3Data, [link]: dynamicLink }); | |
const response = { | |
statusCode: 200, | |
headers, | |
body: JSON.stringify({ link: dynamicLink }), | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment