2015-01-16 13 views
0

我試圖從客戶端的FTP服務器上傳一個大文件(aprrox 2GB到3GB)到我的Amazon S3存儲桶位置。
現在我不允許將整個文件存儲在遠程應用程序服務器中,因此不允許中間停止。除了SFTP之外,我也沒有其他權限訪問客戶端ftp服務器。
你有什麼特別的建議嗎?

另外,現在我使用JSCH圖書館閱讀的FTP服務器作爲InputStream中的文件,然後通過同樣的InputStream到UploadPartRequest。另請注意,我可以使用JSCH庫從客戶端接收文件大小,以便製作多部分文件。 下面是我一直試用的示例代碼。

如何將輸入流從ftp服務器直接傳輸到S3分段上傳?

BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsKey, awsSecretKey); 
    AmazonS3 s3Client = new AmazonS3Client(awsCreds); 
    List<PartETag> partETags = new ArrayList<PartETag>(); 
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(existingBucketName, keyName); 
    InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest); 
    try { 
     long partSize = 50 * 1024 * 1024; 
     jsch= new JSch(); 
     session = jsch.getSession(ftpUserName, ftpLocation, 22); 
     session.setPassword(password); 
     session.setConfig("StrictHostKeyChecking", "no"); 
     session.setTimeout(0); 
     session.connect(); 
     System.out.println("session connected ......" + session.isConnected()); 
     channel = session.openChannel("sftp"); 
     channel.connect(); 
     System.out.println("channel connected...." + channel.isConnected()); 
     c = (ChannelSftp) channel; 
     SftpATTRS attrs = c.lstat(filePath); 
     long contentLength = attrs.getSize(); 
     InputStream is = c.get(filePath); 
     System.out.println("size of the file in remote location is : " + contentLength/(1024*1024) +" MB"); 
     long filePosition = 0; 
     for (int i = 1; filePosition < contentLength; i++) { 
      partSize = Math.min(partSize, (contentLength - filePosition)); 
      UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(existingBucketName) 
        .withKey(keyName).withUploadId(initResponse.getUploadId()) 
        .withPartNumber(i).withFileOffset(filePosition).withInputStream(is).withPartSize(partSize); 
      boolean anotherPass; 
      do { 
       anotherPass = false; 
       try { 
        partETags.add(s3Client.uploadPart(uploadRequest).getPartETag()); 
       } catch (Exception e) { 
        anotherPass = true; 
       } 
      } while (anotherPass); 

     filePosition += partSize; 
      System.out.println("new file pos is : " + filePosition/(1024*1024) +" MB"); 
     } 
     CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(existingBucketName, keyName, initResponse.getUploadId(), partETags); 
     s3Client.completeMultipartUpload(compRequest); 
    } catch (Exception ex){ 
     System.out.println("Exception occurred : " + ex.getMessage()); 
     s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
       existingBucketName, keyName, initResponse.getUploadId())); 
     ex.printStackTrace(); 
    }finally { 
     System.out.println("closing all connections !!!!"); 
     if(session != null){ 
      session.disconnect(); 
     }if(channel != null){ 
      channel.disconnect(); 
     }if(c != null){ 
      c.disconnect(); 
     } 
    } 


與當上載達到中間的某個位置這種方法

現在,那麼整個上傳過程只是停止。該應用程序正在運行,但沒有上傳或下載網絡,上傳停止在50%左右。
任何暗示傢伙???

回答

0

爲了調試這個,我試着看看問題出在哪裏。

首先,您可以解耦此代碼,以便從FTP連接中獲得一個小尺寸的字節數組緩衝區,例如10k,並在每次迭代時丟棄緩衝區(不要發送到S3),以查看如果仍然掛起或不。

如果這個工程後,您可以在同一時間發送此字節塊至S3之一,看看會發生什麼。

+0

那麼問題出現在尺寸大於700Mb。小文件很容易上傳到s3。感謝您的建議:) – haedes

+0

10k在整個2Gb或3Gb – stivlo

相關問題