AmazonS3Client:从s3下载时,jar文件已损坏

时间:2015-09-28 09:54:24

标签: java amazon-s3

我正在尝试使用AmazonS3Client下载jar文件。但是下载后,获取的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

2 个答案:

答案 0 :(得分:0)

您是否尝试手动下载(使用浏览器)然后运行它?

可能是AmazonS3以某种方式损坏了它,而不是你的程序。

答案 1 :(得分: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();