I need to SCP a video file from my local machine to a EC2 cloud server using a Java program. I am using the JSCH library to do so.
So far I can connect to the EC2 server and can run basic commands on it. Following is the code:
import com.jcraft.jsch.*;
import java.io.*;
import java.io.IOException;
public class EC2
{
public static void main(String[] args) throws JSchException, IOException, InterruptedException
{
JSch jsch=new JSch();
jsch.addIdentity("yyyy.pem");
jsch.setConfig("StrictHostKeyChecking", "no");
//enter your own EC2 instance IP here
Session session=jsch.getSession("ec2-user", "xx.xxx.xxx.xxx", 22);
session.connect();
//run stuff
String command = "whoami;hostname";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream input = channel.getInputStream();
//start reading the input from the executed commands on the shell
byte[] tmp = new byte[1024];
while (true)
{
while (input.available() > 0)
{
int i = input.read(tmp, 0, 1024);
if (i < 0) break;
System.out.println(new String(tmp, 0, i));
}
if (channel.isClosed())
{
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
Thread.sleep(1000);
}
channel.disconnect();
session.disconnect();
}
}
Now I am aware of the SCP syntax for file transfer . But the thing here is that every command that I now here will be run the EC2 server For eg: String command = "ls -l"; Will give me the home file list in the EC2.
So how do I transfer the video stored on my laptop the home/ec2-user directory of the EC2 ?