Published
Edited
Mar 19, 2021
2 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
handler = async (req, res, context) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "PUT, GET, OPTIONS");
if (req.method === 'OPTIONS') res.status(200).end(); // Get CORS preflights ACKed ASAP
// Fetch secrets from context
const aws_creds = context.secrets['tomlarkworthy_aws'];
AWS.config.credentials = new AWS.ChainableTemporaryCredentials({
masterCredentials: parseCreds(aws_creds)
});
// A very simple API, GET returns a bucket file, PUT sets one.
const key = req.url.substring(1); // URL has "/" prefix which AWS Bucket keyspace does not
if (req.method == "GET") {
const s3res = await new AWS.S3().getObject({
Bucket: 'tomlarkworthy-access-aws',
Key: key
}).promise().catch((err) => res.status(404).send(err.message))
res.send(s3res.Body.toString('utf-8'))
} else if (req.method == "PUT") {
const s3res = await new AWS.S3().putObject({
Bucket: 'tomlarkworthy-access-aws',
Key: key,
Body: req.body
}).promise()
res.end()
}
}
Insert cell
Insert cell
Insert cell
Insert cell
endpoint = deploy("access-aws", handler, {
secrets: ['tomlarkworthy_aws']
})
Insert cell
Insert cell
Insert cell
Insert cell
suite.test("01: GET <random> 404s", async () => {
const response = await fetch(endpoint.href + "/fdsfdfs");
expect(response.status).toBe(404);
})
Insert cell
suite.test("02: GET /access-aws/example.txt 200 and 'foo'", async () => {
const response = await fetch(endpoint.href + "/access-aws/example.txt");
expect(response.status).toBe(200);
expect(await response.text()).toBe("foo");
})
Insert cell
suite.test("03: PUT /test.txt 200", async () => {
const response = await fetch(endpoint.href + "/access-aws/put.txt", {
method: "PUT",
body: "foo"
});
expect(response.status).toBe(200);
})
Insert cell
suite.test("04: Read your writes", async () => {
const data = Math.random() + "";
await fetch(endpoint.href + "/access-aws/getput.txt", {
method: "PUT",
body: data
});
const response = await fetch(endpoint.href + "/access-aws/getput.txt");
expect(response.status).toBe(200);
expect(await response.text()).toBe(data);
})
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more