File tree 3 files changed +50
-2
lines changed
3 files changed +50
-2
lines changed Original file line number Diff line number Diff line change 1
1
import { AWSSignerV4 } from "../deps.ts" ;
2
- import type { CreateBucketOptions } from "./types.ts" ;
2
+ import type { CreateBucketOptions , DeleteBucketOptions } from "./types.ts" ;
3
3
import { S3Error } from "./error.ts" ;
4
4
import { S3Bucket } from "./bucket.ts" ;
5
5
import { doRequest , encoder } from "./request.ts" ;
@@ -88,4 +88,30 @@ export class S3 {
88
88
bucket,
89
89
} ) ;
90
90
}
91
+
92
+ async deleteBucket (
93
+ bucket : string ,
94
+ options ?: DeleteBucketOptions ,
95
+ ) : Promise < void > {
96
+ const headers : Params = { } ;
97
+
98
+ if ( options ?. expectedBucketOwner ) {
99
+ headers [ "x-amz-expected-bucket-owner" ] = options . expectedBucketOwner ;
100
+ }
101
+
102
+ const resp = await doRequest ( {
103
+ host : this . #host,
104
+ signer : this . #signer,
105
+ path : bucket ,
106
+ method : "DELETE" ,
107
+ headers,
108
+ } ) ;
109
+
110
+ if ( resp . status !== 204 ) {
111
+ throw new S3Error (
112
+ `Failed to delete bucket "${ bucket } ": ${ resp . status } ${ resp . statusText } ` ,
113
+ await resp . text ( ) ,
114
+ ) ;
115
+ }
116
+ }
91
117
}
Original file line number Diff line number Diff line change @@ -27,7 +27,6 @@ Deno.test({
27
27
28
28
// teardown
29
29
await bucket . deleteObject ( "foo" ) ;
30
- // @TODO : delete also bucket once s3.deleteBucket is implemented.
31
30
} ,
32
31
} ) ;
33
32
@@ -42,3 +41,22 @@ Deno.test({
42
41
) ;
43
42
} ,
44
43
} ) ;
44
+
45
+ Deno . test ( {
46
+ name : "[client] should delete a bucket" ,
47
+ async fn ( ) {
48
+ await s3 . deleteBucket ( "test.bucket" ) ;
49
+ } ,
50
+ } ) ;
51
+
52
+ Deno . test ( {
53
+ name :
54
+ "[client] should throw when deleting a bucket if the bucket does not exist" ,
55
+ async fn ( ) {
56
+ await assertThrowsAsync (
57
+ ( ) => s3 . deleteBucket ( "test.bucket" ) ,
58
+ S3Error ,
59
+ 'Failed to delete bucket "test.bucket": 404 Not Found' ,
60
+ ) ;
61
+ } ,
62
+ } ) ;
Original file line number Diff line number Diff line change @@ -566,3 +566,7 @@ export interface CreateBucketOptions {
566
566
/** Allows grantee to write the ACL for the applicable bucket. */
567
567
grantWriteAcp ?: string ;
568
568
}
569
+
570
+ export interface DeleteBucketOptions {
571
+ expectedBucketOwner ?: string ;
572
+ }
You can’t perform that action at this time.
0 commit comments