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(sdk): can retrieve empty object from bucket on AWS platforms #6091

Merged
merged 4 commits into from
Mar 29, 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
17 changes: 13 additions & 4 deletions examples/tests/sdk_tests/bucket/get.test.w
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bring cloud;
bring expect;

let b = new cloud.Bucket();

Expand All @@ -16,14 +17,22 @@ test "get range of an object" {

b.put("test1.txt", "12345");

assert(b.get("test1.txt", startByte: 1, endByte: 3) == "234");
assert(b.get("test1.txt", startByte: 1) == "2345");
assert(b.get("test1.txt", endByte: 3) == "1234");
expect.equal(b.get("test1.txt", startByte: 1, endByte: 3), "234");
expect.equal(b.get("test1.txt", startByte: 1), "2345");
expect.equal(b.get("test1.txt", endByte: 3), "1234");

b.put("test2.txt", "𠮷");

assert(b.get("test2.txt", startByte: 0, endByte: 3) == "𠮷");
expect.equal(b.get("test2.txt", startByte: 0, endByte: 3), "𠮷");
assertThrows("The encoded data was not valid for encoding utf-8", () => {
b.get("test2.txt", startByte: 0, endByte: 2);
});


}

test "get empty object" {
b.put("empty.txt", "");

expect.equal(b.get("empty.txt"), "");
}
17 changes: 12 additions & 5 deletions libs/wingsdk/src/shared-aws/bucket.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DeleteObjectCommand,
GetBucketLocationCommand,
GetObjectCommand,
GetObjectCommandInput,
GetObjectOutput,
GetPublicAccessBlockCommand,
GetPublicAccessBlockCommandOutput,
Expand Down Expand Up @@ -99,13 +100,19 @@ export class BucketClient implements IBucketClient {
key: string,
options?: BucketGetOptions
): Promise<string | undefined> {
const command = new GetObjectCommand({
const getObjectParams: GetObjectCommandInput = {
Bucket: this.bucketName,
Key: key,
Range: `bytes=${
options?.startByte !== undefined ? options.startByte : 0
}-${options?.endByte !== undefined ? options.endByte : ""}`,
});
};

// Conditionally add the `Range` parameter
if (options?.startByte !== undefined || options?.endByte !== undefined) {
const startByte = options?.startByte ?? 0;
const endByte = options?.endByte ?? "";
getObjectParams.Range = `bytes=${startByte}-${endByte}`;
}

const command = new GetObjectCommand(getObjectParams);

try {
const resp: GetObjectOutput = await this.s3Client.send(command);
Expand Down
12 changes: 12 additions & 0 deletions main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;

let logs = new cloud.Bucket();
let fileName = "test.log";

new cloud.OnDeploy(inflight () => {
logs.put(fileName, "");
});

test "get file" {
assert(logs.tryGet(fileName) == "");
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
## stdout.log
```log
pass ─ get.test.wsim » root/env0/test:get range of an object
pass ─ get.test.wsim » root/env1/test:get empty object


Tests 1 passed (1)
Tests 2 passed (2)
Test Files 1 passed (1)
Duration <DURATION>
```
Expand Down
Loading