View Source AlibabaCloudKit.OSS.Object (alibaba_cloud_kit v1.0.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.
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
@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-signature-version": x_oss_signature_version,
"x-oss-credential": x_oss_credential,
"x-oss-date": x_oss_date,
"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)