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