View Source AlibabaCloudKit.OSS.Object (alibaba_cloud_kit v1.1.0)

Provides OSS object related helpers.

It's a high-level module built on top of AlibabaCloudKit.Signature.OSS4.

Summary

Functions

Presigns a PostObject operation and returns the necessary form data when executing the PostObject operation.

Presigns a PostObject operation with OSS V1 signature.

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()}
  | {:at, at()}
@type sign_opts() :: [sign_opt()]

Functions

Link to this function

presign_post_object(conditions, seconds_to_expire, opts)

View Source
@spec presign_post_object(list(), pos_integer(), sign_opts()) :: %{
  policy: String.t(),
  "x-oss-signature-version": String.t(),
  "x-oss-credential": String.t(),
  "x-oss-date": String.t(),
  "x-oss-signature": String.t()
}

Presigns a PostObject operation and returns the necessary form data when executing the PostObject operation.

Read more at:

Examples

region = "oss-us-west-1"
bucket = "example-bucket"

key = "lenna.png"
acl = "private"
max_size_in_bytes = 1024 * 1024 * 5

conditions = [
  ["eq", "$key", key],
  ["eq", "$x-oss-object-acl", acl],
  ["content-length-range", 1, max_size_in_bytes]
]

seconds_to_expire = 3600

opts = [
  access_key_id: "...",
  access_key_secret: "...",
  region: region,
  bucket: bucket
]

%{
  policy: policy,
  "x-oss-credential": x_oss_credential,
  "x-oss-date": x_oss_date,
  "x-oss-signature-version": x_oss_signature_version,
  "x-oss-signature": x_oss_signature,
} = AlibabaCloudKit.OSS.presign_post_object(conditions, seconds_to_expire, opts)

To use the returned data, you should built a form with multipart data, and send it:

alias Tesla.Multipart

url = "https://#{bucket}.#{region}.aliyuncs.com"

multipart =
  Multipart.new()
  |> Multipart.add_field("key", key)
  |> Multipart.add_field("x-oss-object-acl", acl)
  |> Multipart.add_field("policy", policy)
  |> Multipart.add_field("x-oss-credential", x_oss_credential)
  |> Multipart.add_field("x-oss-date", x_oss_date)
  |> Multipart.add_field("x-oss-signature-version", x_oss_signature_version)
  |> Multipart.add_field("x-oss-signature", x_oss_signature)
  |> Multipart.add_file_content(binary_of_file, "lenna.png")

{:ok, %{status: 204}} = Tesla.post(url, multipart)
Link to this function

presign_post_object_v1(conditions, seconds_to_expire, opts)

View Source
@spec presign_post_object_v1(list(), pos_integer(), sign_opts()) :: %{
  policy: String.t(),
  signature: String.t()
}

Presigns a PostObject operation with OSS V1 signature.

OSS V1 signature is only supported at here.

You should this function only for maintaining legacy code. In other case, please use presign_post_object/3.

Examples

region = "oss-us-west-1"
bucket = "example-bucket"

key = "lenna.png"
acl = "private"
max_size_in_bytes = 1024 * 1024 * 5

conditions = [
  ["eq", "$key", key],
  ["eq", "$x-oss-object-acl", acl],
  ["content-length-range", 1, max_size_in_bytes]
]

seconds_to_expire = 3600

opts = [
  access_key_id: "...",
  access_key_secret: "...",
  region: region,
  bucket: bucket
]

%{
  policy: policy,
  signature: signature
} = AlibabaCloudKit.OSS.presign_post_object_v1(conditions, seconds_to_expire, opts)

To use the returned data, you should built a form with multipart data, and send it:

alias Tesla.Multipart

url = "https://#{bucket}.#{region}.aliyuncs.com"

multipart =
  Multipart.new()
  |> Multipart.add_field("key", key)
  |> Multipart.add_field("x-oss-object-acl", acl)
  |> Multipart.add_field("OSSAccessKeyId", access_key_id)
  |> Multipart.add_field("policy", policy)
  |> Multipart.add_field("Signature", signature)
  |> Multipart.add_file_content(binary_of_file, "lenna.png")

{:ok, %{status: 204}} = Tesla.post(url, multipart)