2015-09-28 84 views
0

我正在嘗試使用AmazonS3Client下載jar文件。但下載後,獲取的jar文件已損壞不會執行。AmazonS3Client:從s3下載損壞的jar文件

我使用下面的程序:

String existingBucketName = args[0]; 
    String keyName = "/" + "EMRS3Files-1.0-SNAPSHOT-jar-with-dependencies.jar"; 

    BasicAWSCredentials awsCreds = new BasicAWSCredentials("access-key", "secret-key"); 
    AmazonS3 s3Client = new AmazonS3Client(awsCreds); 

    GetObjectRequest request = new GetObjectRequest(existingBucketName, keyName); 
    S3Object object = s3Client.getObject(request); 
    S3ObjectInputStream objectContent = object.getObjectContent(); 

    IOUtils.copy(objectContent, new FileOutputStream("/tmp/abc.jar")); 

在運行/tmp/abc.jar文件,我得到以下錯誤:

Error: Invalid or corrupt jarfile abc.jar 

回答

0

以下工作:

GetObjectRequest request = new GetObjectRequest(existingBucketName, keyName); 
    S3Object object = s3Client.getObject(request); 
    S3ObjectInputStream objectContent = object.getObjectContent(); 
    FileOutputStream fos = new FileOutputStream("/tmp/abc.jar"); 

    byte[] buffer = new byte[4096]; 

    int buf = 0; 
    while((buf = objectContent.read(buffer)) > 0) 
    { 
     fos.write(buffer, 0, buf); 
    } 
    fos.close(); 
0

您是否嘗試過(使用瀏覽器)手動下載,然後運行它?

它可能是亞馬遜S3以某種方式損壞它,而不是你的程序。

+0

我試了一下。它運行良好。 – nish

+0

@nish hmm。那我不知道。抱歉。 – RubyJunk