forked from ConzorKingKong/cloud-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 853 Bytes
/
index.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
/**
* Important constants
* @param {string} bucket
* @param {string} hostname
*/
const bucket = 'cfredirect'
const hostname = '.s3.amazonaws.com'
/**
* Module Import
* This module lets you access your AWS buckets
* by wrapping your calls with AWS auth headers
* @param {string} accessKeyId
* @param {string} secretAccessKey
*/
import { AwsClient } from 'aws4fetch'
const aws = new AwsClient({
accessKeyId: process.env.accessKeyId,
secretAccessKey: process.env.secretAccessKey,
})
/**
* Redirects your call to an AWS bucket
* @param {Request} request
*/
async function handleRequest(request) {
const { method, body } = request
const url = new URL(request.url)
url.hostname = bucket + hostname
return aws.fetch(url, { method, body })
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})