无法使用java将文件上传到Amazon Cloudsearch

时间:2014-12-07 23:17:28

标签: java amazon-web-services amazon-cloudsearch

我正在构建一个java应用程序,以便使用amazon cloudsearch索引一些json文件。我认为我已经使用了正确的aws文档,但我无法使我的应用程序正常工作。

package com.myPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;
import com.amazonaws.services.cloudsearchdomain.model.UploadDocumentsRequest;
import com.amazonaws.services.cloudsearchdomain.model.UploadDocumentsResult;

public class App 
{
    public static final String ACCESS_KEY = "myAccessKey";
    public static final String SECRET_KEY = "mySecretKey";
    public static final String ENDPOINT = "myDocumentEndpoint";

    public static void main( String[] args ) throws FileNotFoundException
    {
        AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        AmazonCloudSearchDomainClient domain = new AmazonCloudSearchDomainClient(credentials);
        domain.setEndpoint(ENDPOINT);

        File file = new File("path to my file");
        InputStream docAsStream = new FileInputStream(file);
        UploadDocumentsRequest req = new UploadDocumentsRequest();
        req.setDocuments(docAsStream);
        System.out.print(file.length());
        UploadDocumentsResult result = domain.uploadDocuments(req);//here i get the exception

        System.out.println(result.toString());
//      
//      SearchRequest searchReq = new SearchRequest().withQuery("my Search request");
//      SearchResult s_res = domain.search(searchReq);
//      System.out.println(s_res);
    }
}

问题是我收到以下错误:

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to unmarshall error response (Unable to parse error response: '<html><body><h1>403 Forbidden</h1>Request forbidden by administrative rules.</body></html>'). Response Code: 403, Response Text: Forbidden
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1071)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295)
    at com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient.invoke(AmazonCloudSearchDomainClient.java:527)
    at com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient.uploadDocuments(AmazonCloudSearchDomainClient.java:310)
    at gvrhtyhuj.dfgbmn.App.main(App.java:31)
Caused by: com.amazonaws.AmazonClientException: Unable to parse error response: '<html><body><h1>403 Forbidden</h1>Request forbidden by administrative rules.</body></html>'
    at com.amazonaws.http.JsonErrorResponseHandler.handle(JsonErrorResponseHandler.java:55)
    at com.amazonaws.http.JsonErrorResponseHandler.handle(JsonErrorResponseHandler.java:29)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1045)
    ... 6 more
Caused by: com.amazonaws.util.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at com.amazonaws.util.json.JSONTokener.syntaxError(JSONTokener.java:422)
    at com.amazonaws.util.json.JSONObject.<init>(JSONObject.java:196)
    at com.amazonaws.util.json.JSONObject.<init>(JSONObject.java:323)
    at com.amazonaws.http.JsonErrorResponseHandler.handle(JsonErrorResponseHandler.java:53)

这是json文件:

{"a":123,"b":"4 5 6"}

2 个答案:

答案 0 :(得分:0)

您从CloudSearch收到403 Forbidden错误,这意味着您无权将文档上传到该域。

您是否确实使用“myAccessKey”作为访问密钥值,或者在发布此密码时是否对其进行了编辑?如果您从未设置过,那么您需要设置访问密钥;否则,请通过AWS Web控制台检查CloudSearch域上的访问策略,因为它可能配置为根据IP地址或其他一些条件接受/拒绝提交。

答案 1 :(得分:0)

首先:请不要将您的凭据放在您的代码中。很容易意外地将凭证检查到版本控制中,或以其他方式发布它们。如果您的凭据位于默认位置,则可以执行以下操作:

AmazonCloudSearchDomainClient客户端=新的AmazonCloudSearchDomainClient();

SDK会找到它们。

403s的一个常见原因是端点错误。确保端点字符串末尾没有/ documents / batch。 SDK会添加它。

另一件要尝试的是设置内容长度:

    req.setContentLength(file.length());

我的代码有,并且有效,并且与您的代码相同。