Golang example, same principle, run head request again the object in question:
func returnKeySizeInMB(bucketName string, key string) {
output, err := svc.HeadObject(
&s3.HeadObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
})
if err != nil {
log.Fatalf("Unable to to send head request to item %q, %v", e.Detail.RequestParameters.Key, err)
}
return int(*output.ContentLength / 1024 / 1024)
}
Here, the parameter key means the path to the file.
For eg, if the URI of the file is S3://my-personal-bucket/folder1/subfolder1/myfile.pdf, then the syntax would look like:
output, err := svc.HeadObject(
&s3.HeadObjectInput{
Bucket: aws.String("my-personal-bucket"),
Key: aws.String("folder1/subfolder1/myfile.pdf"),
})