我想使用 java sdk 对 GCS 上的存储桶启用日志记录。本质上,我想实现与 gsutil 命令等效的命令:gsutillogging 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 客户端执行此操作,无需使用 gsutil
命令启动子进程:
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();
来源:https://github.com/googleapis/google-cloud-java/issues/6981#issuecomment-561648354 .
关于java - 在java中设置谷歌云存储桶访问日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260652/