带有Java的Google云存储gsutil工具

fox*_*ndy 1 google-cloud-storage

如果我们有大约30G文件(范围从50MB到4GB)需要每天上传到谷歌云存储,根据谷歌文档,gsutil可能是唯一合适的选择,不是吗?

我想用Java调用gsutil命令,现在下面的代码可以工作.但是如果我删除while循环,程序将在runtime.exec(命令)之后立即停止,但python进程已启动但不进行上传,很快就会被终止.我想知道为什么.

我从sterr流中读取的原因是受到管道gsutil输出到文件的启发

我决定gsutil是否通过read util执行其状态输出的最后一行,但它是否可靠?有没有更好的方法来检测gsutil执行是否以Java结尾?

String command="python c:/gsutil/gsutil.py cp C:/SFC_Data/gps.txt"
            + " gs://getest/gps.txt";
 try {
        Process process = Runtime.getRuntime().exec(command);
        System.out.println("the output stream is "+process.getErrorStream());
        BufferedReader reader=new BufferedReader(new InputStreamReader(process.getErrorStream())); 
        String s; 
        while ((s = reader.readLine()) != null){
            System.out.println("The inout stream is " + s);
        }                
    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

小智 6

当然,每天向GCS上传30G价值数据的方法不止一种.由于您使用的是Java,您是否考虑过使用Cloud Storage API Java客户端库? https://developers.google.com/api-client-library/java/apis/storage/v1

至于使用Runtime.exec()从Java调用gsutil的具体问题,我怀疑当没有while循环时,程序将在创建子进程后立即退出,导致"process"变量为GC,这可能会杀死子流程.

我认为你应该等待子流程完成,这实际上就是while循环的作用.或者你可以调用waitFor()并检查existsValue(),如果你不关心输出:http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html