Skip to content

Commit

Permalink
feat: add deleteBucket method
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Nov 7, 2021
1 parent e8daafe commit d03ce2c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AWSSignerV4 } from "../deps.ts";
import type { CreateBucketOptions } from "./types.ts";
import type { CreateBucketOptions, DeleteBucketOptions } from "./types.ts";
import { S3Error } from "./error.ts";
import { S3Bucket } from "./bucket.ts";
import { doRequest, encoder } from "./request.ts";
Expand Down Expand Up @@ -88,4 +88,30 @@ export class S3 {
bucket,
});
}

async deleteBucket(
bucket: string,
options?: DeleteBucketOptions,
): Promise<void> {
const headers: Params = {};

if (options?.expectedBucketOwner) {
headers["x-amz-expected-bucket-owner"] = options.expectedBucketOwner;
}

const resp = await doRequest({
host: this.#host,
signer: this.#signer,
path: bucket,
method: "DELETE",
headers,
});

if (resp.status !== 204) {
throw new S3Error(
`Failed to delete bucket "${bucket}": ${resp.status} ${resp.statusText}`,
await resp.text(),
);
}
}
}
20 changes: 19 additions & 1 deletion src/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Deno.test({

// teardown
await bucket.deleteObject("foo");
// @TODO: delete also bucket once s3.deleteBucket is implemented.
},
});

Expand All @@ -42,3 +41,22 @@ Deno.test({
);
},
});

Deno.test({
name: "[client] should delete a bucket",
async fn() {
await s3.deleteBucket("test.bucket");
},
});

Deno.test({
name:
"[client] should throw when deleting a bucket if the bucket does not exist",
async fn() {
await assertThrowsAsync(
() => s3.deleteBucket("test.bucket"),
S3Error,
'Failed to delete bucket "test.bucket": 404 Not Found',
);
},
});
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,7 @@ export interface CreateBucketOptions {
/** Allows grantee to write the ACL for the applicable bucket. */
grantWriteAcp?: string;
}

export interface DeleteBucketOptions {
expectedBucketOwner?: string;
}

0 comments on commit d03ce2c

Please sign in to comment.