@oazmi/kitchensink - v0.10.1
    Preparing search index...

    Function s3SignHeadersV4

    • this function computes the Authentication field for your S3 http request headers, and then returns a header object (record) that can be used for an AWS Signature Version 4 authenticated S3 server.

      Type Parameters

      • T = Record<string, string>

      Parameters

      Returns Promise<T & { Authorization: string }>

      returns the original headers with an added "Authentication" field. note that you modifying the returned headers will invalidate your authentication key. so you should compute it as the last thing before sending out the request.

      fetching some content from an S3 bucket (for instance, minio bucket).

      const
      method = "GET",
      host = "localhost:9000",
      pathname = "/bucket-name/object-name",
      accessKey = "minioadmin",
      secretKey = "minioadmin",
      endpoint = `https://${host}${pathname}`

      const headers = s3SignHeadersV4(host, pathname, accessKey, secretKey, {
      method,
      headers: { "Content-Type": "text/plain" },
      })

      // usage with fetch:
      // const my_text_object = await (await fetch(s3_endpoint, { method, headers })).text()

      example usage for verifying against amazon's guide page: link.

      import { assert } from "jsr:@std/assert"

      const
      AWSAccessKeyId = "AKIAIOSFODNN7EXAMPLE",
      AWSSecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
      original_headers = { "raNGe": "bytes=0-9" }

      const signed_headers = await s3SignHeadersV4("examplebucket.s3.amazonaws.com", "/test.txt", AWSAccessKeyId, AWSSecretAccessKey, {
      payload: "",
      headers: original_headers,
      date: "20130524T000000Z",
      })

      // expected value: "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, SignedHeaders=host;range;x-amz-content-sha256;x-amz-date, Signature=f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41"
      const authfield = signed_headers.Authorization

      assert(authfield.startsWith("AWS4-HMAC-SHA256"))
      assert(authfield.includes("Credential=" + AWSAccessKeyId))
      assert(authfield.includes("SignedHeaders=host;range;x-amz-content-sha256;x-amz-date"))
      assert(authfield.endsWith("Signature=f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41"))