Is AmazonS3 automatically disconnected?

0

I am a Java engineer designing an "Aws S3" utility class. I now need to upload files in bulk, so in the upload method "uploadFile", I have commented out the close connection statement in "finally". I want to keep this connection multiplexed to avoid the overhead of re-establishing a connection, so I set up a Object pool,the object is Connection. Does AmazonS3 automatically disconnect after being idle for a long time? If the connection presence is automatically closed, will the s3Client in the ClientPool become null, resulting in a null value corresponding to the key? How can I solve these problems? Please help to correct my code, thank you.If you know Chinese, please refer to the original Chinese question to avoid ambiguity. The code is as follows:

    private static Map<String, AmazonS3> ClientPool = new ConcurrentHashMap<>();
    /**
     * 获取 Amazon S3 客户端实例
     * @param region
     * @param accessKeyId
     * @param secretKey
     * @param endpointUrl
     * @return 连接池复用的client
     */
    public static AmazonS3 getS3Client(String region, String accessKeyId, String secretKey, String endpointUrl) {
        String clientKey = region + accessKeyId + secretKey + endpointUrl;
        return ClientPool.computeIfAbsent(clientKey, k -> createS3Client(region, accessKeyId, secretKey, endpointUrl));
    }
    // todo aws服务端自动空闲断连,不用扫描。
    /**
     * S3 建立连接
     *
     * @param region 时区
     * @param accessKeyId 用户私钥
     * @param secretKey 公钥
     * @param endpointUrl 节点位置
     * @return 已连接
     */
    public static AmazonS3 createS3Client(String region, String accessKeyId, String secretKey, String endpointUrl) {
        region = StringUtils.isEmpty(region) ? "us-east-1" : region;
        AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endpointUrl, region);
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, secretKey);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTPS);
        AmazonS3 s3client = AmazonS3Client.builder().withEndpointConfiguration(endpointConfig).withClientConfiguration(clientConfig).withCredentials(awsCredentialsProvider).disableChunkedEncoding().withPathStyleAccessEnabled(true).withForceGlobalBucketAccessEnabled(true).build();
        return s3client;
    }

    /**
     * AWS s3 上传
     *
     * @param targetPullFileName 存放文件 example:"path/path/xx.csv"
     * @param inputStream        文件输入流
     * @return
     */
    public String uploadFile(String targetPullFileName, InputStream inputStream) throws IOException {
        log.info("=======================================开始上传文件=======================================");
        log.info("Upload File: " + targetPullFileName + " To " + bucketName);
        AmazonS3 s3Client = getS3Client(region, accessKeyId, secretKey, endpointUrl);
        try {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(inputStream.available());
            PutObjectRequest request = new PutObjectRequest(bucketName, targetPullFileName, inputStream, metadata);
            s3Client.putObject(request);
        } catch (Exception e) {
            log.error("上传" + targetPullFileName + "文件失败,错误原因为" + e.getMessage());
            targetPullFileName += "上传失败";
            e.printStackTrace();
        } finally {
            inputStream.close();
         //   s3Client.shutdown();
            return targetPullFileName;
        }
    }

我是Java工程师,正在设计一个 ”Aws S3 “工具类。 我现在批量上传文件,所以在上传方法中,我最后注释掉了关闭连接的语句。我想保持这个连接复用以避免重新建立连接的开销,因此我设置了一个连接池。“AmazonS3”在长时间空闲后是否会自动断开连接。如果连接存在被自动关闭了,ClientPool中的s3Client是否会变成null,从而导致与该键对应的值为null?请帮我指出我代码中的问题。

질문됨 4달 전256회 조회
1개 답변
1
수락된 답변

there isn't a concept of a direct "connection" that needs to be opened and closed in the same way as traditional database connections. The SDK abstracts the underlying networking details, and you typically don't need to manage connections explicitly.

Please find the sample file upload using boto3 using python

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/upload_file.html

profile picture
Jagan
답변함 4달 전
  • Thank you for your reply. I found the relevant details in the official document later. It's really a great design. Thank you again for your answer

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠