0
我想通過java文件選擇器將多個文件上傳到Amazon s3桶。爲此,我使用下面的代碼。此代碼可以將文件上傳至s3,但下次我上傳另一個文件時,將替換上一個文件。我知道這是由String key =「squre.jpg」造成的;在代碼中。我的問題是如何上傳多個文件而無需替換上一個。先謝謝了。使用java filechooser在amazon s3桶中上傳多個文件
imageUpload.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
FileChooser fileChooser=new FileChooser();
fileChooser.setInitialDirectory(new File("c:\\"));
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"),
new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"),
new FileChooser.ExtensionFilter("PNG Images","*.png"));
File file=fileChooser.showOpenDialog(null);
if (file!=null){
try {
AWSCredentials Credentials = new BasicAWSCredentials(
"AWSAccessKeyId",
"AWSSecretKey");
AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials);
String bucketName = "awsimagetrading";
String key = "squre.jpg";
System.out.println("Uploading a new object to S3 from a file\n");
AmazonS3 s3client = new AmazonS3Client(Credentials);
s3client.putObject(new PutObjectRequest(bucketName,key,file));
URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES)));
System.out.println(url);
//label.setText(url.toString());
} catch (AmazonClientException e) {
e.printStackTrace();
}
}
}
});
是什麼阻止您爲每次上傳使用不同的密鑰?或者我在這裏錯過了什麼? – GhostCat