繁体   English   中英

在Java中通过HTTP Web服务在SOAP中调用Amazon S3客户端时失败

[英]Amazon S3 client fails when invoked within SOAP over HTTP web service in Java

我有一个将图像加载到亚马逊S3并返回公共URL到该图像的类。 当我将该类作为Java应用程序运行时-一切正常。 当我从上到下的方法创建SOAP Web服务并尝试在Web服务代码中调用该代码时-它会失败。 我确切地知道它在哪里失败,但是我不知道为什么,而且我也无法调试它,即使在尝试捕获错误的情况下也不会打印任何内容到控制台。

我正在获取java.lang.reflect.InvocationTargetException 我在这里和亚马逊论坛上搜索,找不到任何解决该问题的建议(我看到很多与此错误有关的问题,但没有解决方案)。

这是我的类,用于上传图像并获取url,它可以很好地用作Java应用程序:

import java.io.File;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class S3Uploader {
private String url = null;

public S3Uploader(String filePath){
    setUrl(s3uploadAndGetUrl(filePath));
}


public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String s3uploadAndGetUrl(String file) {
    // get access to S3
    //TODO protect amazon credentials, refer to amazon documentation about proper way of providing credentials.
    String result = null;
    URL url;
    try {
        AWSCredentials awsCreds = new BasicAWSCredentials(
                "myCredentials",
                "myCredentials");
        System.out.println("Credentials loaded.");
        AmazonS3Client s3Client = new AmazonS3Client(awsCreds);
        System.out.println("Client created.");

        // image to upload
        File image = new File(file);

        // set expiration date of images' urls
        Calendar origDay = Calendar.getInstance();
        Calendar expirationDay = (Calendar) origDay.clone();
        expirationDay.add(Calendar.DAY_OF_YEAR, 30);
        Date expirationDate = expirationDay.getTime();

        // set bucket and images names
        String bucketName = s3Client.listBuckets().get(0).getName();
        String objectKey = image.getName();

        // upload image
        PutObjectRequest putObject = new PutObjectRequest(bucketName,
                objectKey, image);
        s3Client.putObject(putObject);

        // create and retrieve image url
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                bucketName, objectKey);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expirationDate);

        url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
        result = url.toString();

        System.out.println("Pre-Signed URL = " + url.toString());
        System.out.println();
    } catch (AmazonServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result = e.getCause().getMessage();
    } catch (AmazonClientException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getCause().getMessage());
        result = e.getCause().getMessage();
    }

    return result;
}

}

现在,我在其中调用此类的Web服务代码:

/**
* UrlsCreatorSOAPImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/

public class UrlsCreatorSOAPImpl implements org.example.www.UrlsCreator.UrlsCreator_PortType{
public java.lang.String getImagesUrls(java.lang.String Url) throws java.rmi.RemoteException {

    S3Uploader upload = new S3Uploader("C:/Users/Maksym/Dropbox/Greece/Matala/IMG_20140508_134143.jpg");
    return upload.getUrl();

 }
}

这是唯一打印到控制台的内容:

INFO: Credentials loaded.

也是SOAP请求:

- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.example.org/UrlsCreator/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
 - <q0:GetImagesUrls>
    <Url>asdasdadsa</Url>
   </q0:GetImagesUrls>
 </soapenv:Body>
 </soapenv:Envelope>

和SOAP响应:

- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
  - <soapenv:Fault>
    <faultcode>soapenv:Server.userException</faultcode> 
    <faultstring>java.lang.reflect.InvocationTargetException</faultstring> 
   - <detail>
     <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">Maksym-PC</ns1:hostname> 
   </detail>
  </soapenv:Fault>
 </soapenv:Body>
</soapenv:Envelope>

如果可以的话,请提供帮助,如果我需要提供其他信息,请告诉我。 提前致谢。

更新1:我在2-3天前下载了java aws sdk,所以它应该是最新的。 这是WEB-INF / lib下的jar文件列表:

 aws-java-sdk-1.7.10-javadoc.jar
 aws-java-sdk-1.7.10.jar
 aws-java-sdk-flow-build-tools-1.7.10.jar
 axis.jar
 commons-codec-1.3.jar
 commons-discovery-0.2.jar
 commons-logging-1.1.1.jar
 commons-logging.jar
 httpclient-4.2.3.jar
 httpcore-4.2.jar
 jackson-annotations-2.1.1.jar
 jackson-core-2.1.1.jar
 jackson-databind-2.1.1.jar
 jaxrpc.jar
 joda-time-2.2.jar
 saaj.jar
 wsdl4j.jar

解决方案:事实证明-问题出在玻璃鱼或我的笔记本电脑上,我不知道。 我从头开始尝试使用另一台具有相同eclipse版本,相同glassfish的笔记本电脑,并且可以正常工作,但我仍然不知道笔记本电脑出了什么问题(其他Web服务运行平稳,清理项目和服务器也无济于事)。

无论如何,感谢您的帮助=)

确保在运行代码时使用与开发/编译代码时相同的库API版本。 对于远程S3 API,请确保您在客户端上具有的版本与服务器端版本兼容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM