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

feat: add deleteBucket method #34

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3475364
feat: add createBucket method
c4spar Nov 6, 2021
d96b424
move createBucket method to S3 class
c4spar Nov 7, 2021
58e18d8
remove unused interface
c4spar Nov 7, 2021
86575ac
add missing export in mod.ts
c4spar Nov 7, 2021
e8daafe
rename utils.ts to request.ts
c4spar Nov 7, 2021
d03ce2c
feat: add deleteBucket method
c4spar Nov 7, 2021
1dafea7
make path optional in S3RequestOptions
c4spar Nov 8, 2021
bffc48e
add getBucket method
c4spar Nov 11, 2021
c9c3104
fix example
c4spar Nov 11, 2021
d719bce
re-format
c4spar Nov 11, 2021
ac41170
Merge remote-tracking branch 'origin/feat/create-bucket' into feat/de…
c4spar Nov 11, 2021
00c16b8
update readme
c4spar Nov 11, 2021
c2ee356
add missing export
c4spar Nov 11, 2021
d2d7119
update test's
c4spar Nov 11, 2021
7408059
rename bucket
c4spar Nov 12, 2021
a4806c2
update docs
c4spar Nov 12, 2021
9b39bd0
add CreateBucketConfiguration
c4spar Nov 12, 2021
573c19a
move locationConstraint to CreateBucketOptions
c4spar Nov 12, 2021
7837e4d
export LocationConstraint
c4spar Nov 12, 2021
8ca519e
Revert "move locationConstraint to CreateBucketOptions"
c4spar Nov 12, 2021
9af711f
fix example
c4spar Nov 12, 2021
14fb72c
update docs
c4spar Nov 12, 2021
1bf7d40
fix type
c4spar Nov 12, 2021
75ae119
Merge remote-tracking branch 'origin/feat/create-bucket' into feat/de…
c4spar Nov 12, 2021
ab5e79e
update docs
c4spar Nov 12, 2021
9045d32
cleanup
c4spar Nov 13, 2021
1e616bc
Merge remote-tracking branch 'origin/feat/create-bucket' into feat/de…
c4spar Nov 13, 2021
59cc1b6
Merge remote-tracking branch 'origin/main' into feat/delete-bucket
c4spar Nov 23, 2021
a69884e
fix test
c4spar Nov 23, 2021
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
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type {
CopyObjectResponse,
CreateBucketConfiguration,
CreateBucketOptions,
DeleteBucketOptions,
DeleteObjectOptions,
DeleteObjectResponse,
GetObjectOptions,
Expand Down
37 changes: 36 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 @@ -121,4 +121,39 @@ export class S3 {

return this.getBucket(bucket);
}

/**
* Deletes the S3 bucket. All objects (including all object versions and
* delete markers) in the bucket must be deleted before the bucket itself can
* be deleted.
*
* ```
* await s3.deleteBucket("my-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(),
);
}
}
}
15 changes: 14 additions & 1 deletion src/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ Deno.test({

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

Deno.test({
name: "[client] should delete a bucket",
async fn() {
await s3.createBucket("create-bucket-test");
await s3.deleteBucket("create-bucket-test");
await assertThrowsAsync(
() => s3.deleteBucket("create-bucket-test"),
S3Error,
'Failed to delete bucket "create-bucket-test": 404 Not Found',
);
},
});
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,20 @@ export interface CreateBucketOptions extends CreateBucketConfiguration {

/**
* Allows grantee to create new objects in the bucket.
* For the bucket and object owners of existing objects, also allows deletions and overwrites of those objects.
* For the bucket and object owners of existing objects, also allows deletions
* and overwrites of those objects.
*/
grantWrite?: string;

/** Allows grantee to write the ACL for the applicable bucket. */
grantWriteAcp?: string;
}

export interface DeleteBucketOptions {
/**
* The account ID of the expected bucket owner. If the bucket is owned by a
* different account, the request will fail with an HTTP 403 (Access Denied)
* error.
*/
expectedBucketOwner?: string;
}