bucketExists method

Future<bool> bucketExists(
  1. String bucket
)

Checks if a bucket exists.

Returns true only if the bucket exists and you have the permission to access it. Returns false if the bucket does not exist or you don't have the permission to access it.

Implementation

Future<bool> bucketExists(String bucket) async {
  MinioInvalidBucketNameError.check(bucket);
  try {
    final response = await _client!.request(method: 'HEAD', bucket: bucket);
    validate(response);
    return response.statusCode == 200;
  } on MinioS3Error catch (e) {
    final code = e.error!.code;
    if (code == 'NoSuchBucket' || code == 'NotFound' || code == 'Not Found') {
      return false;
    }
    rethrow;
  } on StateError catch (e) {
    // Insight from testing: in most cases, AWS S3 returns the HTTP status code
    // 404 when a bucket does not exist. Whereas in other cases, when the bucket
    // does not exist, S3 returns the HTTP status code 301 Redirect instead of
    // status code 404 as officially documented. Then, this redirect response
    // lacks the HTTP header `location` which causes this exception in Dart's
    // HTTP library (`http_impl.dart`).
    if (e.message == 'Response has no Location header for redirect') {
      return false;
    }
    rethrow;
  }
}