0

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 ?

1 Answers1

0

Your scp command must be run locally and not on the EC2 Instance. No need of JSCH library for that. See this question for more details How to execute cmd commands via Java

Of course, scp must be locally available too ....

--Seb

Community
  • 1
  • 1
Sébastien Stormacq
  • 14,301
  • 5
  • 41
  • 64
  • Yes, but I need to do a performance test of EC2, so I will eventually push 1000's of videos into it. Hence I am writing a JAVA program to do so. –  Mar 27 '14 at 00:03
  • Sometime a simple shell script is much easier to develop than a Java application. If you have to push a large quantity of data to AWS for processing on an EC2 instance, I would suggest to have a look at S3. Your local machine can upload to s3 (using the CLI or a Java program) and your EC2 program download from S3 before processing the file. – Sébastien Stormacq Mar 27 '14 at 07:13