2017-05-05 4 views
0

Java 파일 선택기로 Amazon s3 버킷에 여러 파일을 업로드하고 싶습니다. 그 목적을 위해 다음 코드를 사용하고 있습니다. 이 코드는 s3에 파일을 업로드 할 수 있지만 다음 번에 다른 파일을 업로드하면 이전 파일이 대체됩니다. 나는 그것이 String key = "squre.jpg"에 의해 발생한다는 것을 안다; 코드에서. 내 질문에 이전 파일을 대체하지 않고 여러 파일을 업로드하는 방법입니다. 고맙습니다. 당신이 filechooser의 openMultipleDialog를 사용할 필요가 다음 파일의 이름을 키 설정할 수 있습니다처럼 코드에서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(); 
       } 
      } 
     } 
    }); 
+0

업로드 할 때마다 다른 키를 사용하지 못하게하는 이유는 무엇입니까? 아니면 여기에 뭔가 빠졌나요? – GhostCat

답변

0

보이는 (file.getName()). 수정 된 코드는 다음과 같습니다.

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")); 

      List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null); 
      if (selectedFiles != null) { 
       for (File file : selectedFiles) { 
        try { 

         AWSCredentials Credentials = new BasicAWSCredentials(
           "AWSAccessKeyId", 
           "AWSSecretKey"); 

         AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials); 
         String bucketName = "awsimagetrading"; 
         String key = file.getName(); 
         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(); 
        } 
       } 
     } 
    }); 
+0

고맙습니다. 이제 작동 중입니다. @jshcode – Sony

관련 문제