【问题标题】:Why not shutdown the s3 client in the code?为什么不关闭代码中的 s3 客户端?
【发布时间】:2023-01-09 12:40:37
【问题描述】:

我正在寻找 aws sdk 指南文档。

下面的代码解释了 aws sdk 的 s3 用法。

在 finally 子句中,我看不到 S3 client 的关闭。 它有 shutdown 方法来释放它持有的资源。 但是关闭资源在官方文档中是看不到的。

我的假设是否正确?或者还有其他我错过的事情吗?

public class GetObject2 {

    public static void main(String[] args) throws IOException {
        Regions clientRegion = Regions.DEFAULT_REGION;
        String bucketName = "*** Bucket name ***";
        String key = "*** Object key ***";

        S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Get an object and print its contents.
            System.out.println("Downloading an object");
            fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
            System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
            System.out.println("Content: ");
            displayTextInputStream(fullObject.getObjectContent());

            // Get a range of bytes from an object and print the bytes.
            GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key)
                    .withRange(0, 9);
            objectPortion = s3Client.getObject(rangeObjectRequest);
            System.out.println("Printing bytes retrieved.");
            displayTextInputStream(objectPortion.getObjectContent());

            // Get an entire object, overriding the specified response headers, and print the object's content.
            ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
                    .withCacheControl("No-cache")
                    .withContentDisposition("attachment; filename=example.txt");
            GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName, key)
                    .withResponseHeaders(headerOverrides);
            headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
            displayTextInputStream(headerOverrideObject.getObjectContent());
        } catch (AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        } finally {
            // To ensure that the network connection doesn't remain open, close any open input streams.
            if (fullObject != null) {
                fullObject.close();
            }
            if (objectPortion != null) {
                objectPortion.close();
            }
            if (headerOverrideObject != null) {
                headerOverrideObject.close();
            }
        }
    }

    private static void displayTextInputStream(InputStream input) throws IOException {
        // Read the text input stream one line at a time and display each line.
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println();
    }
}

【问题讨论】:

    标签: amazon-s3 aws-sdk


    【解决方案1】:

    来自 SDK documentationshutdown()AmazonS3 类的描述:

    关闭此客户端对象,释放可能保持打开状态的任何资源。这是一个可选方法,调用者不应调用它,但如果他们想显式释放任何打开的资源,则可以调用它。客户端关闭后,不应再使用它发出任何请求。

    对 S3 的请求是通过单独的 REST API 调用完成的,因此客户端不会像您期望的数据库客户端那样保持连续的连接/会话。它只是使用提供给它的配置数据构造和签署每个请求。这可能就是为什么调用 shutdown 方法对 S3 客户端不那么重要的原因。

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2020-09-30
      • 2010-11-16
      • 2021-02-13
      相关资源
      最近更新 更多