Skip to content
Open
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
20 changes: 20 additions & 0 deletions packages/@emulators/aws/src/__tests__/aws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ describe("AWS plugin - S3 Objects", () => {
expect(res.headers.get("Content-Type")).toBe("text/plain");
});

it("puts and gets binary data intact", async () => {
const binaryData = Buffer.from([0x00, 0x01, 0x02, 0xff, 0xfe, 0xfd, 0x80, 0x7f]);
const putRes = await app.request(`${base}/s3/emulate-default/binary.bin`, {
method: "PUT",
headers: { ...authHeaders(), "Content-Type": "application/octet-stream" },
body: binaryData,
});
expect(putRes.status).toBe(200);

const getRes = await app.request(`${base}/s3/emulate-default/binary.bin`, {
method: "GET",
headers: authHeaders(),
});
expect(getRes.status).toBe(200);
const responseBuffer = Buffer.from(await getRes.arrayBuffer());
expect(responseBuffer).toEqual(binaryData);
expect(getRes.headers.get("Content-Length")).toBe(String(binaryData.length));
expect(getRes.headers.get("Content-Type")).toBe("application/octet-stream");
});

it("copies an object with x-amz-copy-source", async () => {
await app.request(`${base}/s3/emulate-default/source.txt`, {
method: "PUT",
Expand Down
2 changes: 1 addition & 1 deletion packages/@emulators/aws/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function generateReceiptHandle(): string {
return randomBytes(48).toString("base64url");
}

export function md5(content: string): string {
export function md5(content: string | Buffer): string {
return createHash("md5").update(content).digest("hex");
}

Expand Down
11 changes: 6 additions & 5 deletions packages/@emulators/aws/src/routes/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ ${prefixesXml}
return awsXmlResponse(c, xml);
}

const body = await c.req.text();
const bodyBuffer = Buffer.from(await c.req.arrayBuffer());
const body = bodyBuffer.toString("base64");
const contentType = c.req.header("Content-Type") ?? "application/octet-stream";
const etag = md5(body);
const etag = md5(bodyBuffer);

// Extract user metadata (x-amz-meta-*)
const metadata: Record<string, string> = {};
Expand All @@ -235,7 +236,7 @@ ${prefixesXml}
aws().s3Objects.update(existing.id, {
body,
content_type: contentType,
content_length: new TextEncoder().encode(body).byteLength,
content_length: bodyBuffer.byteLength,
etag,
last_modified: new Date().toISOString(),
metadata,
Expand All @@ -246,7 +247,7 @@ ${prefixesXml}
key,
body,
content_type: contentType,
content_length: new TextEncoder().encode(body).byteLength,
content_length: bodyBuffer.byteLength,
etag,
last_modified: new Date().toISOString(),
metadata,
Expand Down Expand Up @@ -284,7 +285,7 @@ ${prefixesXml}
headers[`x-amz-meta-${k}`] = v;
}

return c.text(obj.body, 200, headers);
return c.body(Buffer.from(obj.body, "base64"), 200, headers);
});

// HeadObject - HEAD /s3/:bucket/:key{.+}
Expand Down