HeadObjectCommand vs GetObjectCommand in AWS SDK

🧠 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. ...

November 6, 2025 Β· 3 min