-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
53 lines (40 loc) · 1.29 KB
/
s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { AwsClient } = require("./dependencies/aws4fetch/aws4fetch");
const { getInput } = require("./github-action");
const AWS_ACCESS_KEY_ID = getInput("aws-access-key-id");
const AWS_SECRET_ACCESS_KEY = getInput("aws-secret-access-key");
const AWS_REGION = getInput("aws-region");
const AWS_CACHE_BUCKET = getInput("aws-cache-bucket");
const client = new AwsClient({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
region: AWS_REGION,
service: "s3",
});
const put = async (key, body) => {
const res = await client.fetch(
`https://${AWS_CACHE_BUCKET}.s3.${AWS_REGION}.amazonaws.com/${key}`,
{
method: "PUT",
body,
}
);
if (!res.ok) throw new Error(res.statusText);
};
const exist = async (key) => {
const res = await client.fetch(
`https://${AWS_CACHE_BUCKET}.s3.${AWS_REGION}.amazonaws.com/${key}`,
{ method: "HEAD" }
);
if (res.status === 404) return false;
if (!res.ok) throw new Error(res.statusText);
return true;
};
const get = async (key) => {
const res = await client.fetch(
`https://${AWS_CACHE_BUCKET}.s3.${AWS_REGION}.amazonaws.com/${key}`
);
if (res.status === 404) return null;
if (!res.ok) throw new Error(res.statusText);
return await res.arrayBuffer();
};
module.exports = { get, put, exist };