简体   繁体   English

为什么不关闭代码中的 s3 客户端?

[英]Why not shutdown the s3 client in the code?

I am looking for the aws sdk guide documentation.我正在寻找 aws sdk 指南文档。

Below code explains the s3 usage for aws sdk.下面的代码解释了 aws sdk 的 s3 用法。

In the finally clause, I cannot see the shutdown for the S3 client .在 finally 子句中,我看不到S3 client的关闭。 It has shutdown method for releasing the resource it held.它有shutdown方法来释放它持有的资源。 But the closing the resources cannot be seen in the official document.但是关闭资源在官方文档中是看不到的。

My assumption is correct?我的假设是否正确? Or is there any other thing that I have missed?还是我错过了其他任何事情?

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();
    }
}

Description of shutdown() from the SDK documentation for AmazonS3 class: AmazonS3 class 的SDK 文档中对shutdown()的描述:

Shuts down this client object, releasing any resources that might be held open.关闭此客户端 object,释放所有可能保持打开状态的资源。 This is an optional method, and callers are not expected to call it, but can if they want to explicitly release any open resources.这是一个可选方法,调用者不应调用它,但如果他们想显式释放任何打开的资源,则可以调用它。 Once a client has been shutdown, it should not be used to make any more requests.客户端关闭后,不应再使用它发出任何请求。

Requests to S3 are done via individual REST API calls, so the client doesn't maintain a continuous connection/session as you'd expect from a database client for example.对 S3 的请求是通过单独的 REST API 调用完成的,因此客户端不会像您期望的数据库客户端那样保持连续的连接/会话。 It simply constructs and signs each request using the configuration data provided to it.它只是使用提供给它的配置数据构造和签署每个请求。 This is probably why calling the shutdown method is not as important for the S3 client.这可能就是为什么调用shutdown方法对于 S3 客户端不那么重要的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM