View Source AlibabaCloudKit.Signature.OSS4 (alibaba_cloud_kit v1.1.1)

An implementation for OSS V4 signature.

This type of signature is used by:

  • Object Storage Service (OSS)

Read more at:

Summary

Functions

Signs a request.

Types

@type access_key_id() :: String.t()
@type access_key_secret() :: String.t()
@type at() :: DateTime.t() | nil
@type bucket() :: String.t() | nil
@type region() :: String.t()
@type sign_opt() ::
  {:access_key_id, access_key_id()}
  | {:access_key_secret, access_key_secret()}
  | {:region, region()}
  | {:bucket, bucket()}
  | {:sign_type, sign_type()}
  | {:at, at()}
@type sign_opts() :: [sign_opt()]
@type sign_type() :: :header | :query

Functions

Signs a request.

The location to put signature

The location to put signature is controlled by the :sign_type option:

  • :header (default) - add signature to the headers of a request.
  • :query - add signature to the query of a request.

Automatically added request headers when using :header sign type

Following headers will be added to the request automatically:

  • host
  • date
  • content-md5 (overridable)
  • content-type (overridable)
  • x-oss-date
  • x-oss-content-sha256
  • authorization

Automatically added request queries when using :query sign type

Following queries will be added to the request automatically:

  • x-oss-signature-version
  • x-oss-credential
  • x-oss-date
  • x-oss-expires (overridable)
  • x-oss-additional-headers

Required headers

All necessary headers of requests will be generated automatically. You don't have to specifically set them, unless you want to customize it.

Examples

Build and sign a request for ListBucktets operation

request = HTTPSpec.Request.new!(
  method: :get,
  scheme: :https,
  host: "oss-us-west-1.aliyuncs.com",
  port: 443,
  path: "/"
)

opts = [
  access_key_id: "...",
  access_key_secret: "...",
  region: "oss-us-west-1",
  sign_type: :header
]

AlibabaCloudKit.Signature.OSS4.sign!(request, opts)

Build a pre-signed url for GetObject operation

request = HTTPSpec.Request.new!(
  method: :get,
  scheme: :https,
  host: "example-bucket.oss-us-west-1.aliyuncs.com",
  port: 443,
  path: "/example-object",
  query: "x-oss-expires=900"
)

opts = [
  access_key_id: "...",
  access_key_secret: "...",
  region: "oss-us-west-1",
  bucket: "example-bucket",
  sign_type: :query
]

request
|> AlibabaCloudKit.Signature.OSS4.sign!(opts)
|> HTTPSpec.Request.build_url()