-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some read-only endpoints for getting and listing files. (#25)
This provides an HTTP-based alternative to accessing the R2 bucket via the S3 API. It supports downloading individual files, getting the HEAD, and listing the contents of the bucket. The aim is to allow web apps to just get/list files via the usual fetch() calls instead of being forced to bundle the AWS S3 SDK.
- Loading branch information
Showing
6 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as http from "./utils/http.js"; | ||
import * as s3 from "./utils/s3.js"; | ||
|
||
function createHeaders(payload) { | ||
const headers = new Headers(); | ||
payload.writeHttpMetadata(headers); | ||
headers.set('etag', payload.httpEtag); | ||
headers.set('Last-Modified', payload.uploaded.toUTCString()); | ||
headers.set("Content-Length", payload.size); | ||
return headers; | ||
} | ||
|
||
export async function headFileHandler(request, env, nonblockers) { | ||
const payload = await env.BOUND_BUCKET.head(decodeURIComponent(request.params.key)); | ||
if (payload === null) { | ||
throw new http.HttpError("object not found", 404); | ||
} | ||
const headers = createHeaders(payload); | ||
return new Response(null, { headers }); | ||
} | ||
|
||
export async function downloadFileHandler(request, env, nonblockers) { | ||
const payload = await env.BOUND_BUCKET.get(decodeURIComponent(request.params.key)); | ||
if (payload === null) { | ||
throw new http.HttpError("object not found", 404); | ||
} | ||
const headers = createHeaders(payload); | ||
return new Response(payload.body, { headers }); | ||
} | ||
|
||
export async function listFilesHandler(request, env, nonblockers) { | ||
const params = request.query; | ||
let prefix = null; | ||
if ("prefix" in params) { | ||
prefix = decodeURIComponent(params.prefix); | ||
} | ||
let recursive = false; | ||
if ("recursive" in params) { | ||
recursive = params.recursive == "true"; | ||
} | ||
let collected = []; | ||
await s3.listApply(prefix, x => collected.push(x), env, { trimPrefix: false, stripTrailingSlash: false, local: !recursive }); | ||
return new http.jsonResponse(collected, 200); | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as read from "../src/read.js"; | ||
import * as setup from "./setup.js"; | ||
|
||
beforeAll(async () => { | ||
const env = getMiniflareBindings(); | ||
await setup.simpleMockProject(env); | ||
}) | ||
|
||
test("headFileHandler works correctly", async () => { | ||
const env = getMiniflareBindings(); | ||
const req = new Request("http://localhost", { method: "HEAD" }); | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/whee.txt") }; | ||
const res = await read.headFileHandler(req, env, []); | ||
expect(res.body).toBeNull(); | ||
const hh = res.headers; | ||
expect(hh.get("Last-Modified").length).toBeGreaterThan(0); | ||
expect(hh.get("etag").length).toBeGreaterThan(0); | ||
expect(Number(hh.get("Content-Length"))).toBeGreaterThan(0); | ||
} | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/..summary") }; | ||
const res = await read.headFileHandler(req, env, []); | ||
expect(res.body).toBeNull(); | ||
const hh = res.headers; | ||
expect(hh.get("content-type")).toBe("application/json"); | ||
expect(Number(hh.get("Content-Length"))).toBeGreaterThan(0); | ||
} | ||
}) | ||
|
||
test("downloadFileHandler works correctly", async () => { | ||
const env = getMiniflareBindings(); | ||
const req = new Request("http://localhost", { method: "GET" }); | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/whee.txt") }; | ||
const res = await read.downloadFileHandler(req, env, []); | ||
const body = await res.text(); | ||
expect(body.startsWith("Aaron")).toBe(true); | ||
|
||
const hh = res.headers; | ||
expect(hh.get("Last-Modified").length).toBeGreaterThan(0); | ||
expect(hh.get("etag").length).toBeGreaterThan(0); | ||
expect(Number(hh.get("Content-Length"))).toBeGreaterThan(0); | ||
} | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/..summary") }; | ||
const res = await read.downloadFileHandler(req, env, []); | ||
const body = await res.json(); | ||
expect("upload_user_id" in body).toBe(true); | ||
|
||
const hh = res.headers; | ||
expect(hh.get("content-type")).toBe("application/json"); | ||
} | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/foo/bar.txt") }; | ||
const res = await read.downloadFileHandler(req, env, []); | ||
const body = await res.text(); | ||
expect(body.startsWith("1\n")).toBe(true); | ||
} | ||
|
||
{ | ||
req.params = { key: encodeURIComponent("test/blob/v1/absent.txt") }; | ||
await expect(read.downloadFileHandler(req, env, [])).rejects.toThrow("not found"); | ||
} | ||
}) | ||
|
||
test("listFilesHandler works correctly", async () => { | ||
const env = getMiniflareBindings(); | ||
const req = new Request("http://localhost", { method: "GET" }); | ||
|
||
{ | ||
req.query = {}; | ||
const res = await read.listFilesHandler(req, env, []); | ||
const body = await res.json(); | ||
expect(body.indexOf("test/") >= 0).toBe(true); | ||
} | ||
|
||
{ | ||
req.query = { prefix: encodeURIComponent("test/blob") }; | ||
const res = await read.listFilesHandler(req, env, []); | ||
const body = await res.json(); | ||
expect(body.indexOf("test/blob/") >= 0).toBe(true); | ||
} | ||
|
||
{ | ||
req.query = { prefix: encodeURIComponent("test/blob/") }; | ||
const res = await read.listFilesHandler(req, env, []); | ||
const body = await res.json(); | ||
expect(body.indexOf("test/blob/..latest") >= 0).toBe(true); | ||
expect(body.indexOf("test/blob/v1/") >= 0).toBe(true); | ||
} | ||
|
||
{ | ||
req.query = { prefix: encodeURIComponent("test/blob"), recursive: "true" }; | ||
const res = await read.listFilesHandler(req, env, []); | ||
const body = await res.json(); | ||
expect(body.indexOf("test/blob/v1/..summary") >= 0).toBe(true); | ||
expect(body.indexOf("test/blob/v1/foo/bar.txt") >= 0).toBe(true); | ||
expect(body.indexOf("test/..permissions") == -1).toBe(true); | ||
} | ||
}) |
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