我想使用带有进程构建器的 aws s3 cli 命令从 s3 存储桶下载文件。代码如下。如果我不想对文件名进行硬编码,则会收到错误消息。谁能告诉我如何在命令中传递变量。
public static void main(String[] args) {
ProcessBuilder processBuilder = new ProcessBuilder();
Map<String, String> env = processBuilder.environment();
System.out.println(env);
String fileName = "NBN-20210623000000000001";
// Checking if the system is windows or not
if (System.getProperty("os.name")
.startsWith("Windows")) {
// processBuilder.command("cmd.exe", "/c", "echo %cd%");
// Need to pass the cli commands to download the file based on the --profile
processBuilder.command("cmd.exe", "/c",
"aws s3 cp s3://npis-sam-deal-calculator/estimate/ASI000000000042/AsynchronousProcess/${fileName} src/main/resources/ --profile npis-dev-np");
} else {
// In pipeline the environment is on unix so need to get the profile from the
// environment variable
processBuilder.command("/bin/bash", "-c",
"aws s3 cp s3://npis-sam-deal-calculator/pricelist/dcpl.csv src/test/resources/ --profile ${aws_profile}");
}
try {
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
如果你看到了,我在fileName
. 但是, fileName
在 Windows 的 s3 命令中使用我无法下载该文件。可以做些什么来解决这个问题?