Skip to content

Commit ac41170

Browse files
committed
Merge remote-tracking branch 'origin/feat/create-bucket' into feat/delete-bucket
2 parents d03ce2c + d719bce commit ac41170

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const s3 = new S3({
2121
endpointURL: Deno.env.get("S3_ENDPOINT_URL"),
2222
});
2323

24-
const myBucket = s3.createBucket("my-bucket", { acl: "private" });
24+
const myBucket = await s3.createBucket("my-bucket", { acl: "private" });
2525

2626
// Create a bucket instance from an existing bucket.
2727
const bucket = new S3Bucket({

src/bucket.ts

-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ export class S3Bucket {
284284
const res = await doRequest({
285285
host: this.#host,
286286
signer: this.#signer,
287-
path: `/`,
288287
method: "GET",
289288
params,
290289
headers,

src/client.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export class S3 {
2828
this.#config = { ...config };
2929
}
3030

31+
getBucket(bucket: string): S3Bucket {
32+
return new S3Bucket({
33+
...this.#config,
34+
bucket,
35+
});
36+
}
37+
3138
async createBucket(
3239
bucket: string,
3340
options?: CreateBucketOptions,
@@ -83,10 +90,7 @@ export class S3 {
8390
// clean up http body
8491
await resp.arrayBuffer();
8592

86-
return new S3Bucket({
87-
...this.#config,
88-
bucket,
89-
});
93+
return this.getBucket(bucket);
9094
}
9195

9296
async deleteBucket(

src/client_test.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ const s3 = new S3({
1111
endpointURL: Deno.env.get("S3_ENDPOINT_URL"),
1212
});
1313

14+
Deno.test({
15+
name: "[client] should get an existing bucket",
16+
async fn() {
17+
const bucket = await s3.getBucket("test");
18+
assert(bucket instanceof S3Bucket);
19+
20+
// Check if returned bucket instance is working.
21+
await bucket.putObject("test", encoder.encode("test"));
22+
const resp = await bucket.getObject("test");
23+
const body = await new Response(resp?.body).text();
24+
assertEquals(body, "test");
25+
26+
// teardown
27+
await bucket.deleteObject("test");
28+
},
29+
});
30+
1431
Deno.test({
1532
name: "[client] should create a new bucket",
1633
async fn() {
@@ -20,13 +37,13 @@ Deno.test({
2037
assert(bucket instanceof S3Bucket);
2138

2239
// Check if returned bucket instance is working.
23-
await bucket.putObject("foo", encoder.encode("bar"));
24-
const resp = await bucket.getObject("foo");
40+
await bucket.putObject("test", encoder.encode("test"));
41+
const resp = await bucket.getObject("test");
2542
const body = await new Response(resp?.body).text();
26-
assertEquals(body, "bar");
43+
assertEquals(body, "test");
2744

2845
// teardown
29-
await bucket.deleteObject("foo");
46+
await bucket.deleteObject("test");
3047
},
3148
});
3249

src/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const encoder = new TextEncoder();
1010
interface S3RequestOptions {
1111
host: string;
1212
signer: Signer;
13-
path: string;
1413
method: string;
14+
path?: string;
1515
params?: Params;
1616
headers?: Params;
1717
body?: Uint8Array | undefined;
@@ -20,7 +20,7 @@ interface S3RequestOptions {
2020
export async function doRequest({
2121
host,
2222
signer,
23-
path,
23+
path = "/",
2424
params,
2525
method,
2626
headers,

0 commit comments

Comments
 (0)