Get the Size of an Amazon S3 Object
Updated: Oct 5, 2026
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
ACCESS_KEY,SECRET_KEYandBUCKETidentify the account and bucket;KEYis the object to inspect.- An
AmazonS3FileSystemis created with basic credentials ands3.open()connects. s3.exists(BUCKET, KEY)returns true when the object is present.s3.getFileSize(BUCKET, KEY)returns the object's size in bytes (it returns -1 for a missing key, which theexists()check avoids here), and the result is printed.s3.close()in thefinallyblock disconnects.
Console Output
output/trades.csv is 48213 bytes
When the key is absent the example prints output/trades.csv does not exist instead.
