通过 Java API 在 GCS(Google 云存储)上压缩文件

Gzipping file on GCS (Google Cloud Storage) via Java API

我定期将日志文件放入 GCS 存储桶中(例如 gs://my-bucket/log.json)我想设置一个 java 进程来处理这些文件,对它们进行 gzip 压缩,然后将它们移动到一个单独的存储桶中我存档文件的地方(即将其移动到 gs://archived-logs/my-bucket/log.json.gz

gsutil cp -z 似乎是我目前唯一能找到的选项。有没有人使用他们的 Java API 以可行的方式实施它?

好的,我想我解决了。标准流解决方案在结尾。


    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);


    }