apache/beam · error · UnsupportedOperationException

Can't get filename from root path in the bucket: %s

Error message

Can't get filename from root path in the bucket: %s

What it means

GcsPath models 'gs://bucket/object' paths; getFileName() returns the last component (the object name). If the path has fewer than two components (a bucket root like gs://bucket/), there is no filename, so an UnsupportedOperationException is thrown.

Source

Thrown at sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/gcsfs/GcsPath.java:255

    return fs;
  }

  // Absolute paths are those that have a bucket and the root path.
  @Override
  public boolean isAbsolute() {
    return !bucket.isEmpty() || object.isEmpty();
  }

  @Override
  public GcsPath getRoot() {
    return new GcsPath(fs, "", "");
  }

  @Override
  public GcsPath getFileName() {
    int nameCount = getNameCount();
    if (nameCount < 2) {
      throw new UnsupportedOperationException(
          "Can't get filename from root path in the bucket: " + this);
    }
    return getName(nameCount - 1);
  }

  /**
   * Returns the <em>parent path</em>, or {@code null} if this path does not have a parent.
   *
   * <p>Returns a path that ends in '/', as the parent path always refers to a directory.
   */
  @Override
  public GcsPath getParent() {
    if (bucket.isEmpty() && object.isEmpty()) {
      // The root path has no parent, by definition.
      return null;
    }

    if (object.isEmpty()) {

View on GitHub (pinned to 12126d8942)

Solutions

  1. Check getNameCount() >= 2 before calling getFileName()
  2. If working at bucket level, use getBucket() instead of getFileName()
  3. Avoid constructing paths without an object component when a filename will be needed

Example fix

// before
String name = path.getFileName().toString();
// after
String name = path.getNameCount() >= 2
    ? path.getFileName().toString()
    : path.getBucket();
Defensive patterns

Strategy: type-guard

Validate before calling

if (path.getNameCount() < 2) { throw new IllegalArgumentException("Expected gs://bucket/object, got " + path); }

Type guard

boolean hasFileName(GcsPath p) { return p.getNameCount() >= 2; }

Try / catch

try { return path.getFileName().toString(); } catch (UnsupportedOperationException e) { return path.getBucket(); }

Prevention

When it happens

Trigger: Calling getFileName() on a GcsPath whose getNameCount() < 2, i.e. a path that is only a scheme+bucket with no object component (e.g. GcsPath.fromUri("gs://my-bucket/")).

Common situations: Listing or iterating a bucket root path and then asking for the file name; recursive path utilities that assume non-root paths; object() API call path that derives the filename.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/cbda2372e8e65f73. Report an issue: GitHub.