apache/hadoop · error · IllegalArgumentException

Wrong FS {pathUri} -expected {fsUri}

Error message

Wrong FS {pathUri} -expected {fsUri}

What it means

S3xLoginHelper.checkPath implements FileSystem.checkPath for the S3 filesystems: relative paths are accepted; otherwise the path scheme is compared (case-insensitively) with the filesystem's scheme, and when schemes match the hosts are compared after canonicalizing ports and patching a host-less path from fs.defaultFS. Any mismatch ends in IllegalArgumentException 'Wrong FS <pathUri> -expected <fsUri>', with any user:password auth details deliberately stripped from the message. This is the S3A form of Hadoop's classic 'Wrong FS' error.

Source

Thrown at hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3native/S3xLoginHelper.java:149

        if (equalsIgnoreCase(thisScheme, defaultUri.getScheme())) {
          pathUri = defaultUri; // schemes match, so use this uri instead
        } else {
          pathUri = null; // can't determine auth of the path
        }
      }
      if (pathUri != null) {
        // canonicalize uri before comparing with this fs
        pathUri = canonicalizeUri(pathUri, defaultPort);
        thatHost = pathUri.getHost();
        if (thisHost == thatHost ||       // hosts match
            (thisHost != null &&
                 equalsIgnoreCase(thisHost, thatHost))) {
          return;
        }
      }
    }
    // make sure the exception strips out any auth details
    throw new IllegalArgumentException(
        "Wrong FS " + pathUri + " -expected " + fsUri);
  }

  /**
   * Simple tuple of login details.
   */
  public static class Login {
    private final String user;
    private final String password;

    /**
     * Create an instance with no login details.
     * Calls to {@link #hasLogin()} return false.
     */
    public Login() {
      this("", "");
    }

View on GitHub (pinned to 2add963021)

Solutions

  1. Derive the filesystem from the path itself: FileSystem fs = path.getFileSystem(conf) - never reuse an instance bound to another bucket
  2. Qualify paths against the filesystem you will use: path = path.makeQualified(fs.getUri(), new Path("/"))
  3. Route operations by scheme+host and keep one FileSystem instance per bucket
  4. Do not embed secrets in URIs - the exception strips them from the message, but the underlying config should not contain them

Example fix

// before
FileSystem fs = FileSystem.get(new URI("s3a://bucket-a"), conf);
fs.open(new Path("s3a://bucket-b/file")); // Wrong FS
// after
Path p = new Path("s3a://bucket-b/file");
FileSystem fs = p.getFileSystem(conf); // instance bound to bucket-b
fs.open(p);
Defensive patterns

Strategy: validation

Validate before calling

static boolean pathMatchesFs(Configuration conf, URI fsUri, Path path, int defaultPort) {
  URI p = path.toUri();
  if (p.getScheme() == null) return true;
  URI canonFs = S3xLoginHelper.canonicalizeUri(fsUri, defaultPort);
  return p.getScheme().equalsIgnoreCase(canonFs.getScheme())
      && (p.getHost() == null || p.getHost().equalsIgnoreCase(canonFs.getHost()));
}
// use: if (!pathMatchesFs(conf, fs.getUri(), p, -1)) { fs = p.getFileSystem(conf); }

Try / catch

try {
  fs.open(p);
} catch (IllegalArgumentException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Wrong FS")) {
    fs = p.getFileSystem(conf); // right filesystem for this path
    fs.open(p);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Obtaining one S3AFileSystem (e.g. FileSystem.get(new URI("s3a://bucket-a"), conf)) and calling an operation with a fully-qualified path of a different bucket or scheme, such as fs.open(new Path("s3a://bucket-b/file")); passing an hdfs:// path to an S3A instance; a path whose host cannot be patched because fs.defaultFS has a different scheme.

Common situations: Cached FileSystem instances reused across buckets in Spark/Hive/MapReduce jobs; hardcoded absolute URIs from another environment; mixed s3a:// and s3n:// or wasb:// references in one job; wrong fs.defaultFS in the configuration.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/a0908ca57a6005c0. Report an issue: GitHub.