1

I am trying to get the size of an obejct in S3. I know I can do it via GET but I want to use the HEAD method. But I can't find in the AWS SDK HeadObjectPresignRequest only GetObjectPresignRequest. Using HEAD on the presigned URL gives me a 403

Right now what I am doing is Cancelling the fetch request

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

2

We need to pass headObject to getSignedUrl method itself.

JavaScript:

s3.getSignedUrl(
  "headObject",
  {
    Bucket: "my-test-bucket",
    Key: "path/to/my/object",
  },
  (error, url) => {
    if (error) console.log("error", error);
    if (url) console.log("url", url);
  }
);

Java:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("us-east-1").build();       
URL url = s3Client.generatePresignedUrl(new GeneratePresignedUrlRequest("my-test-bucket", "path/to/my/object",HttpMethod.HEAD));
System.out.println(url.toString());     

Then HEAD request on the object.

curl --location --head 'https://my-test-bucket.s3.amazonaws.com/myobject?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA....Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210418T230000Z&X-Amz-Expires=900&X-Amz-Security-Token=....%2B31dbfIXMQ%3D%3D&X-Amz-Signature=...ad&X-Amz-SignedHeaders=host'
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42