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

    Function s3PresignUrlV4

    • this function computes a pre-signed url for an S3 request (via query parameters), so that it is accepted by an S3 server that uses AWS Signature Version 4 for authentication.

      Type Parameters

      • T = Record<string, string>

      Parameters

      • host: string

        the domain name or host name of the server, not including the http uri scheme.

      • pathname: string

        path of the location you wish to access.

      • accessKey: string

        access key or user's username

      • secretKey: string

        secret key or user's password

      • Optionalconfig: S3PresignUrlV4Config<T>

        additional optional configuration. see S3PresignUrlV4Config for details.

      Returns Promise<string>

      returns a string representing the full pre-signed url.

      sharing an S3 bucket text-file-only upload link with with your client/friend.

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

      const presigned_url = await s3PresignUrlV4(host, pathname, accessKey, secretKey, {
      scheme: "https",
      method,
      expires: 3600, // valid for 1 hour
      // the addition of the canonical header below will force our client to only upload text files.
      headers: { "Content-Type": "text/plain" },
      })

      // this is how your client will now use the pre-signed url to upload/PUT a text file via fetch:
      const is_client = false
      if (is_client) {
      const file_body = "hello world"
      const s3_response = await fetch(presigned_url, {
      method: "PUT",
      headers: { "content-type": "text/plain" }, // header-keys are case-insensitive
      body: file_body,
      })
      }

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

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

      const
      host = "examplebucket.s3.amazonaws.com",
      pathname = "/test.txt",
      AWSAccessKeyId = "AKIAIOSFODNN7EXAMPLE",
      AWSSecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

      const presigned_url: string = await s3PresignUrlV4(host, pathname, AWSAccessKeyId, AWSSecretAccessKey, {
      scheme: "https",
      method: "GET",
      expires: 86400, // valid for 24 hours
      date: "20130524T000000Z",
      })

      // expected value for `presigned_url`:
      // "https://examplebucket.s3.amazonaws.com/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404"
      const params = new URL(presigned_url).searchParams

      assertEquals(params.get("X-Amz-Algorithm"), "AWS4-HMAC-SHA256")
      assertEquals(params.get("X-Amz-Credential"), `${AWSAccessKeyId}/20130524/us-east-1/s3/aws4_request`)
      assertEquals(params.get("X-Amz-SignedHeaders"), "host")
      assertEquals(params.get("X-Amz-Signature"), "aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404")