diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java index b1471e417f9..34e4d78f8cf 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java @@ -306,11 +306,12 @@ public class S3Repository extends BlobStoreRepository { String basePath = getValue(metadata.settings(), settings, Repository.BASE_PATH_SETTING, Repositories.BASE_PATH_SETTING); if (Strings.hasLength(basePath)) { - BlobPath path = new BlobPath(); - for(String elem : basePath.split("/")) { - path = path.add(elem); + if (basePath.startsWith("/")) { + basePath = basePath.substring(1); + deprecationLogger.deprecated("S3 repository base_path trimming the leading `/`, and " + + "leading `/` will not be supported for the S3 repository in future releases"); } - this.basePath = path; + this.basePath = new BlobPath().add(basePath); } else { this.basePath = BlobPath.cleanPath(); } diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java index f8940c6158c..915183888b6 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java @@ -104,4 +104,17 @@ public class S3RepositoryTests extends ESTestCase { Exception e = expectThrows(clazz, () -> new S3Repository(metadata, Settings.EMPTY, new DummyS3Service())); assertThat(e.getMessage(), containsString(msg)); } + + public void testBasePathSetting() throws IOException { + RepositoryMetaData metadata = new RepositoryMetaData("dummy-repo", "mock", Settings.builder() + .put(Repository.BASE_PATH_SETTING.getKey(), "/foo/bar").build()); + S3Repository s3repo = new S3Repository(metadata, Settings.EMPTY, new DummyS3Service()); + assertEquals("foo/bar/", s3repo.basePath().buildAsString()); // make sure leading `/` is removed and trailing is added + + metadata = new RepositoryMetaData("dummy-repo", "mock", Settings.EMPTY); + Settings settings = Settings.builder().put(Repositories.BASE_PATH_SETTING.getKey(), "/foo/bar").build(); + s3repo = new S3Repository(metadata, settings, new DummyS3Service()); + assertEquals("foo/bar/", s3repo.basePath().buildAsString()); // make sure leading `/` is removed and trailing is added + } + }