Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/create-bucket' into feat/de…
Browse files Browse the repository at this point in the history
…lete-bucket
  • Loading branch information
c4spar committed Nov 11, 2021
2 parents d03ce2c + d719bce commit ac41170
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const s3 = new S3({
endpointURL: Deno.env.get("S3_ENDPOINT_URL"),
});

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

// Create a bucket instance from an existing bucket.
const bucket = new S3Bucket({
Expand Down
1 change: 0 additions & 1 deletion src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ export class S3Bucket {
const res = await doRequest({
host: this.#host,
signer: this.#signer,
path: `/`,
method: "GET",
params,
headers,
Expand Down
12 changes: 8 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export class S3 {
this.#config = { ...config };
}

getBucket(bucket: string): S3Bucket {
return new S3Bucket({
...this.#config,
bucket,
});
}

async createBucket(
bucket: string,
options?: CreateBucketOptions,
Expand Down Expand Up @@ -83,10 +90,7 @@ export class S3 {
// clean up http body
await resp.arrayBuffer();

return new S3Bucket({
...this.#config,
bucket,
});
return this.getBucket(bucket);
}

async deleteBucket(
Expand Down
25 changes: 21 additions & 4 deletions src/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ const s3 = new S3({
endpointURL: Deno.env.get("S3_ENDPOINT_URL"),
});

Deno.test({
name: "[client] should get an existing bucket",
async fn() {
const bucket = await s3.getBucket("test");
assert(bucket instanceof S3Bucket);

// Check if returned bucket instance is working.
await bucket.putObject("test", encoder.encode("test"));
const resp = await bucket.getObject("test");
const body = await new Response(resp?.body).text();
assertEquals(body, "test");

// teardown
await bucket.deleteObject("test");
},
});

Deno.test({
name: "[client] should create a new bucket",
async fn() {
Expand All @@ -20,13 +37,13 @@ Deno.test({
assert(bucket instanceof S3Bucket);

// Check if returned bucket instance is working.
await bucket.putObject("foo", encoder.encode("bar"));
const resp = await bucket.getObject("foo");
await bucket.putObject("test", encoder.encode("test"));
const resp = await bucket.getObject("test");
const body = await new Response(resp?.body).text();
assertEquals(body, "bar");
assertEquals(body, "test");

// teardown
await bucket.deleteObject("foo");
await bucket.deleteObject("test");
},
});

Expand Down
4 changes: 2 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const encoder = new TextEncoder();
interface S3RequestOptions {
host: string;
signer: Signer;
path: string;
method: string;
path?: string;
params?: Params;
headers?: Params;
body?: Uint8Array | undefined;
Expand All @@ -20,7 +20,7 @@ interface S3RequestOptions {
export async function doRequest({
host,
signer,
path,
path = "/",
params,
method,
headers,
Expand Down

0 comments on commit ac41170

Please sign in to comment.