File tree Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 11import { AWSSignerV4 } from "../deps.ts" ;
2- import type { CreateBucketOptions } from "./types.ts" ;
2+ import type {
3+ CreateBucketOptions ,
4+ HeadBucketOptions ,
5+ HeadBucketResponse ,
6+ } from "./types.ts" ;
37import { S3Error } from "./error.ts" ;
48import { S3Bucket } from "./bucket.ts" ;
59import { doRequest , encoder } from "./utils.ts" ;
@@ -28,6 +32,37 @@ export class S3 {
2832 this . #config = { ...config } ;
2933 }
3034
35+ async headBucket (
36+ bucket : string ,
37+ options ?: HeadBucketOptions ,
38+ ) : Promise < HeadBucketResponse > {
39+ const headers : Params = { } ;
40+
41+ if ( options ?. expectedBucketOwner ) {
42+ headers [ "x-amz-expected-bucket-owner " ] = options . expectedBucketOwner ;
43+ }
44+
45+ const resp = await doRequest ( {
46+ host : this . #host,
47+ signer : this . #signer,
48+ path : bucket ,
49+ method : "HEAD" ,
50+ headers,
51+ } ) ;
52+
53+ if ( resp . status !== 200 ) {
54+ throw new S3Error (
55+ `Failed to get bucket "${ bucket } ": ${ resp . status } ${ resp . statusText } ` ,
56+ await resp . text ( ) ,
57+ ) ;
58+ }
59+
60+ return {
61+ bucketRegion : resp . headers . get ( "x-amz-bucket-region" ) ?? undefined ,
62+ accessPointAlias : resp . headers . get ( "x-amz-access-point-alias" ) === "true" ,
63+ } ;
64+ }
65+
3166 async createBucket (
3267 bucket : string ,
3368 options ?: CreateBucketOptions ,
Original file line number Diff line number Diff line change @@ -11,6 +11,29 @@ const s3 = new S3({
1111 endpointURL : Deno . env . get ( "S3_ENDPOINT_URL" ) ,
1212} ) ;
1313
14+ Deno . test ( {
15+ name : "[client] should get a bucket" ,
16+ async fn ( ) {
17+ const resp = await s3 . headBucket ( "test" ) ;
18+ assertEquals ( resp , {
19+ bucketRegion : undefined ,
20+ accessPointAlias : false ,
21+ } ) ;
22+ } ,
23+ } ) ;
24+
25+ Deno . test ( {
26+ name :
27+ "[client] should throw when getting a bucket if the bucket does not exist" ,
28+ async fn ( ) {
29+ await assertThrowsAsync (
30+ ( ) => s3 . headBucket ( "not-existing-bucket" ) ,
31+ S3Error ,
32+ 'Failed to get bucket "not-existing-bucket": 404 Not Found' ,
33+ ) ;
34+ } ,
35+ } ) ;
36+
1437Deno . test ( {
1538 name : "[client] should create a new bucket" ,
1639 async fn ( ) {
Original file line number Diff line number Diff line change @@ -537,6 +537,15 @@ export interface DeleteObjectResponse {
537537 deleteMarker : boolean ;
538538}
539539
540+ export interface HeadBucketOptions {
541+ expectedBucketOwner ?: string ;
542+ }
543+
544+ export interface HeadBucketResponse {
545+ bucketRegion ?: string ;
546+ accessPointAlias : boolean ;
547+ }
548+
540549export interface CreateBucketOptions {
541550 /** The canned ACL to apply to the bucket */
542551 acl ?:
You can’t perform that action at this time.
0 commit comments