Get the Size of an Amazon S3 Object

This example shows how to check that an object exists in an Amazon S3 bucket and read its size without downloading it. Both exists() and getFileSize() on AmazonS3FileSystem only fetch the object's metadata, which makes them cheap enough to run before deciding whether to transfer a large file or to verify that an upload completed.

Java Code Listing

package com.northconcepts.datapipeline.examples.amazons3;

import com.northconcepts.datapipeline.amazons3.AmazonS3FileSystem;

public class GetTheSizeOfAnAmazonS3Object {

    private static final String ACCESS_KEY = "YOUR ACCESS KEY";
    private static final String SECRET_KEY = "YOUR SECRET KEY";
    private static final String BUCKET = "YOUR BUCKET";
    private static final String KEY = "output/trades.csv";

    public static void main(String[] args) throws Throwable {
        AmazonS3FileSystem s3 = new AmazonS3FileSystem()
                .setBasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        s3.open();
        try {
            if (s3.exists(BUCKET, KEY)) {
                long size = s3.getFileSize(BUCKET, KEY);
                System.out.println(KEY + " is " + size + " bytes");
            } else {
                System.out.println(KEY + " does not exist");
            }
        } finally {
            s3.close();
        }
    }

}

Code Walkthrough

  1. ACCESS_KEY, SECRET_KEY and BUCKET identify the account and bucket; KEY is the object to inspect.
  2. An AmazonS3FileSystem is created with basic credentials and s3.open() connects.
  3. s3.exists(BUCKET, KEY) returns true when the object is present.
  4. s3.getFileSize(BUCKET, KEY) returns the object's size in bytes (it returns -1 for a missing key, which the exists() check avoids here), and the result is printed.
  5. s3.close() in the finally block disconnects.

Console Output

output/trades.csv is 48213 bytes

When the key is absent the example prints output/trades.csv does not exist instead.

Mobile Analytics