【发布时间】:2020-02-17 19:59:51
【问题描述】:
我想使用 java sdk 对 GCS 上的存储桶启用日志记录。本质上,我想实现 gsutil 命令的等效项:gsutil logging set on -b gs://logging-bucket -o AccessLog gs://mycompanybucket,如文档 here 中指定的那样。我曾尝试通过 java 代码调用命令行,但这总是永远存在。
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();
}
}
【问题讨论】:
标签: java google-cloud-platform google-cloud-storage