S3StorageDriver implements BackupStorageDriver { String getPrefixKey(BackupRestoreContext ctx) throws URISyntaxException { URI uri = new URI(ctx.getExternalLocation()); String[] segments = uri.getPath().split("/"); int startIndex = uri.getScheme().equals(AmazonS3Client.S3_SERVICE_NAME) ? 1 : 2; String prefixKey = ""; for (int i=startIndex; i<segments.length; i++) { prefixKey += segments[i]; if (i < segments.length - 1) { prefixKey += "/"; } } prefixKey = (prefixKey.length() > 0 && !prefixKey.endsWith("/")) ? prefixKey + "/" : prefixKey; prefixKey += ctx.getName(); return prefixKey; }  @Override void upload(BackupRestoreContext ctx); @Override void uploadSchema(BackupRestoreContext ctx, String schema); @Override void download(BackupRestoreContext ctx); @Override String downloadSchema(BackupRestoreContext ctx);  }
@Test public void testGetEmptyPrefixKeyS3Protocol() throws URISyntaxException { String backupName = "backup-name"; BackupRestoreContext backupRestoreContext = BackupRestoreContext.create( "node-id", backupName, "s3: "local-location", "account-id", "secret-key", false, "existing"); Assert.assertEquals(backupName, s3StorageDriver.getPrefixKey(backupRestoreContext)); }
@Test public void testGetNestedPrefixKeyS3Protocol() throws URISyntaxException { String backupName = "backup-name"; String nestedPath = "nested-path"; BackupRestoreContext backupRestoreContext = BackupRestoreContext.create( "node-id", backupName, "s3: "local-location", "account-id", "secret-key", false, "existing"); Assert.assertEquals(nestedPath + "/" + backupName, s3StorageDriver.getPrefixKey(backupRestoreContext)); }
@Test public void testGetEmptyPrefixKeyHTTPSProtocol() throws URISyntaxException { String backupName = "backup-name"; BackupRestoreContext backupRestoreContext = BackupRestoreContext.create( "node-id", backupName, "https: "local-location", "account-id", "secret-key", false, "existing"); Assert.assertEquals(backupName, s3StorageDriver.getPrefixKey(backupRestoreContext)); }
@Test public void testGetNestedPrefixKeyHTTPSProtocol() throws URISyntaxException { String backupName = "backup-name"; String nestedPath = "nested-path/hi/yah"; BackupRestoreContext backupRestoreContext = BackupRestoreContext.create( "node-id", backupName, "https: "local-location", "account-id", "secret-key", false, "existing"); Assert.assertEquals(nestedPath + "/" + backupName, s3StorageDriver.getPrefixKey(backupRestoreContext)); }