0
votes

I want to enable logging against a bucket on GCS using the java sdk. Essentially, I want to implement the equivalent of the gsutil command : gsutil logging set on -b gs://logging-bucket -o AccessLog gs://mycompanybucket as specified in the documentation here . I have tried to call the command line via java code but this always hands forever.

public static void checkLoggingBuckets(ArrayList<String> cloudbuckets) throws IOException {
            Runtime rt = Runtime.getRuntime();
            ArrayList<String> bucketList = cloudbuckets;
            try {
                for (String st: bucketList) {
                    String command = "cmd /c cmd.exe gsutil logging set on -b gs://logs-bucket-2019  -o AccessLog  gs://";
                    System.out.println("checking for bucket "  + st);
                    Process proc = rt.exec(command + st);

            int result = proc.waitFor();

            System.out.println("Process exit code: " + result);
            System.out.println();
            System.out.println("Result:");
            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
                }

    } catch (IOException e) {
        e.printStackTrace();
    }
}
1

1 Answers

1
votes

You can do that with the Java client, there is no need to start a subprocess with gsutil command :

Storage storage = StorageOptions.getDefaultInstance().getService();
Bucket bucket = storage.get("mycompanybucket");

BucketInfo.Logging logging = BucketInfo.Logging.newBuilder()
    .setLogBucket("logging-bucket")  // -b parameter of gsutil logging set command
    .setLogObjectPrefix("AccessLog") // -o parameter of gsutil logging set command
    .build();

bucket = bucket.toBuilder()
    .setLogging(logging)
    .build()
    .update();

Source : https://github.com/googleapis/google-cloud-java/issues/6981#issuecomment-561648354.