ecological-engine-smart-exe.../src/main/java/org/gcube/dataanalysis/executor/util/DataTransferer.java

50 lines
1.6 KiB
Java

package org.gcube.dataanalysis.executor.util;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.gcube.data.transfer.library.DataTransferClient;
import org.gcube.data.transfer.library.TransferResult;
import org.gcube.data.transfer.model.Destination;
import org.gcube.data.transfer.model.PluginInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataTransferer {
private static final Logger logger = LoggerFactory.getLogger(DataTransferer.class);
// returns the number of transferred bytes
public static long transferFileToService(String scope, String username, String host, int port, String fileAbsolutePath, String remoteFolder) throws Exception {
logger.debug("Transferring file {} to {}:{} " , fileAbsolutePath, host, port );
DataTransferClient client=DataTransferClient.getInstanceByEndpoint("http://"+host+":"+port);
File localFile = new File(fileAbsolutePath);
if (!localFile.exists())
throw new Exception("Local file does not exist: " + localFile);
//String localfolder = localFile.getParent();
String file = localFile.getName();
Map<String,String> params=new HashMap<>();
params.put("DESTINATION", remoteFolder);
params.put("SOURCE_PARAMETER", PluginInvocation.DESTINATION_FILE_PATH);
Destination dest=new Destination(file);
TransferResult tranferResult = client.localFile(localFile,dest,Collections.singleton(new PluginInvocation("DECOMPRESS",params)));
logger.debug("Transferring...");
return tranferResult.getTransferedBytes();
}
}