Skip to content

Commit

Permalink
fix(s3): use / as separator (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 27, 2024
1 parent 5f7ff67 commit d85092a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/drivers/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default defineDriver((options: S3DriverOptions) => {

const baseURL = `${options.endpoint.replace(/\/$/, "")}/${options.bucket || ""}`;

const url = (key: string = "") => `${baseURL}/${normalizeKey(key)}`;
const url = (key: string = "") => `${baseURL}/${normalizeKey(key, "/")}`;

const awsFetch = async (url: string, opts?: RequestInit) => {
const request = await getAwsClient().sign(url, opts);
Expand Down
7 changes: 5 additions & 2 deletions src/drivers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ export function defineDriver<OptionsT = any, InstanceT = never>(
return factory;
}

export function normalizeKey(key: string | undefined): string {
export function normalizeKey(
key: string | undefined,
sep: ":" | "/" = ":"
): string {
if (!key) {
return "";
}
return key.replace(/[/\\]/g, ":").replace(/^:|:$/g, "");
return key.replace(/[:/\\]/g, sep).replace(/^[:/\\]|[:/\\]$/g, "");
}

export function joinKeys(...keys: string[]) {
Expand Down
23 changes: 22 additions & 1 deletion test/drivers/s3.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe } from "vitest";
import { describe, it, expect } from "vitest";
import s3Driver from "../../src/drivers/s3";
import { testDriver } from "./utils";
import { AwsClient } from "aws4fetch";

const accessKeyId = process.env.VITE_S3_ACCESS_KEY_ID;
const secretAccessKey = process.env.VITE_S3_SECRET_ACCESS_KEY;
Expand All @@ -20,5 +21,25 @@ describe.skipIf(
endpoint: endpoint!,
region: region!,
}),
additionalTests(ctx) {
it("can access directly with / separator", async () => {
await ctx.storage.set("foo/bar:baz", "ok");
expect(await ctx.storage.get("foo/bar:baz")).toBe("ok");

const client = new AwsClient({
accessKeyId: accessKeyId!,
secretAccessKey: secretAccessKey!,
region,
});
const response = await client.fetch(
`${endpoint}/${bucket}/foo/bar/baz`,
{
method: "GET",
}
);
expect(response.status).toBe(200);
expect(await response.text()).toBe("ok");
});
},
});
});

0 comments on commit d85092a

Please sign in to comment.