🧠 Introduction When working with Amazon S3, two common commands developers use are HeadObjectCommand and GetObjectCommand.
At first glance, they might seem similar — both retrieve object information — but they serve different purposes.
Let’s explore how they differ and when to use each.
⚙️ HeadObjectCommand HeadObjectCommand retrieves only the metadata of an S3 object, without downloading the file content.
This is ideal when you just want to:
Check if a file exists. Get metadata (like Content-Type, Content-Length, Last-Modified, ETag, etc.). Verify object accessibility or permissions. ✅ Example import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3"; const s3 = new S3Client({ region: "ap-southeast-1" }); async function checkFileInfo() { try { const command = new HeadObjectCommand({ Bucket: "my-bucket", Key: "example/data.json", }); const response = await s3.send(command); console.log("✅ Object found:", response); } catch (err) { if (err.name === "NotFound") { console.log("❌ Object not found"); } else { console.error("⚠️ Error:", err); } } } checkFileInfo(); 💡 Output (sample) { "AcceptRanges": "bytes", "LastModified": "2025-10-31T02:45:10.000Z", "ContentLength": 24987, "ContentType": "application/json", "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"" } 👉 No file data is returned — only metadata.
...