2017-01-05 42 views
1

我將日誌文件定期放入GCS存儲桶(例如gs://my-bucket/log.json)我想設置一個java進程來處理這些文件,將它們gzip並將它們移動到單獨的存檔文件的存檔(即將其移動到gs://archived-logs/my-bucket/log.json.gz通過Java API在GCS(Google雲存儲)上對文件進行Gzip文件

gsutil cp -z似乎是我目前可以找到的唯一選項。有沒有人使用他們的Java API以可行的方式實現它?

回答

0

好吧,我想我解決了它。標準流解決方案的結尾。

 

    GcsOutputChannel gcsOutputChannel = gcsService.createOrReplace(new GcsFilename("my-bucket", "log.json.gz"), 
    new GcsFileOptions.Builder().build()); 
    GZIPOutputStream outputStream = new GZIPOutputStream(Channels.newOutputStream(gcsOutputChannel)); 

    GcsInputChannel inputChannel = gcsService 
        .openReadChannel(new GcsFilename("my-bucket", "log.json"), 10000); 

    InputStream inStream = Channels.newInputStream(inputChannel); 
    byte[] byteArr = new byte[10000]; 
    while (inStream.read(byteArr) > 0) { 
     outputStream.write(byteArr); 


    } 

相關問題