16

What is the protocol for closing an aws s3 client connection?

@Override
public boolean connect() {

    if (connected)
        return false;
    else
        s3Client = new AmazonS3Client(credentials);
    return true;
}

@Override
public boolean diconnect() {
    // what should take place here? 
    return false;
}
visc
  • 4,794
  • 6
  • 32
  • 58

3 Answers3

26

You don't need to close a 'connection", as there's no such thing as a continuous connection to S3 when using AmazonS3Client.

The AWS java SDK send REST requests to S3, where REST is stateless, for each REST request, it will be signed with the user credentials information, so it doesn't need a long connection(such as something like session).

visc
  • 4,794
  • 6
  • 32
  • 58
Matt
  • 6,010
  • 25
  • 36
  • 1
    This is no longer true in aws-sdk v3. The v3 `S3Client` will hold a keep-alive connection to S3 open until you call `.destroy()` – Rich Mar 10 '23 at 14:06
13

In the documentation there is an optional method called 'shutdown'

Shuts down this client object, releasing any resources that might be held open. 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.

For example

@Override
public boolean disconnect() {
    s3Client.shutdown()
    return false;
}
J00MZ
  • 675
  • 1
  • 10
  • 27
David Weinberg
  • 1,033
  • 1
  • 13
  • 29
  • 4
    Note this is defined in the `AmazonWebServiceClient` interface. If you have an instance of `AmazonS3` (the interface, which is returned by the `AmazonS3ClientBuilder`), this method is not available. – Jeff Evans Aug 26 '20 at 18:41
2

According to the official documentation:

Service clients in the SDK are thread-safe and, for best performance, you should treat them as long-lived objects. Each client has its own connection pool resource. Explicitly shut down clients when they are no longer needed to avoid resource leaks.

Consider this, if it lives in a service, you're probably fine leaving it open while your app runs. If you create new clients all the time, you should shut them down to avoid memory leaks. In this case, you should run s3Client.shutdown();

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
siRtobey
  • 21
  • 2
  • I'm using it only to putObject like s3client.putObject(bucketName, id, data); is there a chance this causing memory issues. I'm facing java heap but not sure if it's this on ly – Ajeetkumar Jan 21 '22 at 17:58