【问题标题】:Rekognition client keeps rebuilding on each requestRekognition 客户端会根据每个请求不断重建
【发布时间】:2020-05-25 05:45:48
【问题描述】:

我正在使用 AWS Rekognition 构建一个应用程序,我意识到每次我向服务发出请求时,都会不断重新建立与 aws 的连接,这会降低性能。有没有办法让一个连接在整个会话中持续存在?我的代码如下:

private static final AmazonRekognition rekognitionClient = RekognitionUtil.setupRekognitionClient();

private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {

    AWSCredentialsProvider provider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    LOG.info("Access key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
                }

                @Override
                public String getAWSSecretKey() {
                    LOG.info("Secret key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
                }
            };
        }

        @Override
        public void refresh() {

        }
    };

    return provider;

}


private static AmazonRekognition setupRekognitionClient() {

    AWSCredentialsProvider provider = setupCredentials("xxxx", "xxxx");

    return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();

}

  private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {

    AWSCredentialsProvider provider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    LOG.info("Access key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
                }

                @Override
                public String getAWSSecretKey() {
                    LOG.info("Secret key: "  + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
                    return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
                }
            };
        }

        @Override
        public void refresh() {

        }
    };

    return provider;

}


private static AmazonRekognition setupRekognitionClient() {

    AWSCredentialsProvider provider = setupCredentials("xxxx", "xxx");

    return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();

}




 public static String searchCollectionByFace(String collectionId, ByteBuffer sourceByteBuffer) throws Exception {

    LOG.info("Searching face collection by face...");

    String faceId = "";

    try {

        ObjectMapper objectMapper = new ObjectMapper();

        // Get an image object from S3 bucket.
        Image image = new Image().withBytes(sourceByteBuffer);

        // Search collection for faces similar to the largest face in the image.
        SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest().withCollectionId(collectionId).withImage(image).withFaceMatchThreshold(70F).withMaxFaces(2);

        SearchFacesByImageResult searchFacesByImageResult = rekognitionClient.searchFacesByImage(searchFacesByImageRequest);

        List<FaceMatch> faceImageMatches = searchFacesByImageResult.getFaceMatches();

        for (FaceMatch face : faceImageMatches) {
            LOG.info(face.getFace().getFaceId());
            if(face.getFace().getConfidence() > SIMILARITY_LIMIT){
                faceId = face.getFace().getFaceId();
            }
        }

        return faceId;

    } catch (Exception ex) {

        LOG.error("Error has occurred searching for face", ex);
        throw new Exception();

    }

}

【问题讨论】:

标签: java amazon-web-services amazon-s3 amazon-ec2 amazon-rekognition


【解决方案1】:

你可以尝试微调:

在客户端配置中你pass to the client

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多