【问题标题】:403 forbidden error while getting indexes from AWS Elastic search using AWS SDK使用 AWS 开发工具包从 AWS 弹性搜索中获取索引时出现 403 禁止错误
【发布时间】:2018-09-08 04:48:01
【问题描述】:

我正在使用 AWS 开发工具包连接到弹性搜索。我关注https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-indexing.html

本主题中的其他问题更像是权限问题或写入弹性搜索时的问题。对我来说写作工作正常,只有阅读会给我错误

POST 方法工作正常,它正在添加索引。我正在以相同的方式发出 GET 搜索请求

  public void getIndexedRecords(String index,String type,String id,String documentJSON){

        try {
            String endpoint = host +"/" +index+"/_search" ;
            // Builds the request. We need an AWS service, URI, HTTP method, and request
            // body (in this case, JSON).

            Request<?> request = new DefaultRequest<Void>(service);
            request.setEndpoint(new URI(endpoint));
            request.setHttpMethod(HttpMethodName.GET);
            request.setContent(new ByteArrayInputStream(documentJSON.getBytes()));

            // Retrieves our credentials from the computer. For more information on where
            // this class looks for credentials, see
            // http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html.

            String accessKey = CommonUtils.getCommonProperty("accessKey", "");
            String secretKey = CommonUtils.getCommonProperty("secretKey", "");;
            AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);

            // Signs the request using our region, service, and credentials. AWS4Signer
            // modifies the original request rather than returning a new request.

            AWS4Signer signer = new AWS4Signer();
            signer.setRegionName(region);
            signer.setServiceName(service);
            signer.sign(request, creds);
            request.addHeader("Content-Type", "application/json");

            // Creates and configures the HTTP client, creates the error and response
            // handlers, and finally executes the request.

            ClientConfiguration config = new ClientConfiguration();
            config.setProtocol(Protocol.HTTPS);
            AmazonHttpClient client = new AmazonHttpClient(config);
            System.out.println(client);
            ExecutionContext context = new ExecutionContext(true);
            ESAWSErrorHandler errorHandler = new ESAWSErrorHandler();
            ESAWSResponseHandler<Void> responseHandler = new ESAWSResponseHandler<Void>();
            client.requestExecutionBuilder().executionContext(context).errorResponseHandler(errorHandler).request(request)
                    .execute(responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

但它给了我一个 403 禁止错误。当我使用端点 url 执行 Curl XGET 时,我得到了结果。我在这里做错了什么?

com.amazonaws.AmazonServiceException:(服务:空;状态代码: 403;错误代码:禁止;请求 ID:空)在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)

【问题讨论】:

    标签: java amazon-web-services elasticsearch aws-sdk


    【解决方案1】:

    【讨论】:

    • 当我更改 request.setContent(new ByteArrayInputStream(documentJSON.getBytes()));到 request.setContent(new ByteArrayInputStream("".getBytes()));
    • 我想在请求中发送查询参数(排序、搜索等)有没有我可以关注的文档?
    • 这是一个很好的起点elastic.co/guide/en/elasticsearch/reference/current/…如果有帮助请接受答案;)
    • 如何在 AmazonHttpRequest 中发送查询参数?我尝试在请求正文中发送 json,但没有成功。也许我可以为此提出一个单独的问题
    • stackoverflow.com/questions/49556491/… 。你能帮我解决这个问题吗?
    猜你喜欢
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2018-02-14
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多