Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(s3): use / as separator #545

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
},
});
});
Loading