Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defp signature(signing_key, string_to_sign) do
- hmac_sha256(signing_key, string_to_sign)
- |> bytes_to_string
- end
- defp signing_key(secret_key, date, region) do
- hmac_sha256("AWS4#{secret_key}", date)
- |> hmac_sha256(region)
- |> hmac_sha256(@service)
- |> hmac_sha256(@aws_request)
- end
- def hmac_sha256(key, data) do
- :crypto.hmac(:sha256, key, data)
- end
- def bytes_to_string(bytes) do
- Base.encode16(bytes, case: :lower)
- end
- defp policy(key, mimetype, credential, date, expiration_window \ 60) do
- %{
- expiration: now_plus(expiration_window),
- conditions: [
- %{bucket: bucket_name},
- ["starts-with", "$key", key],
- %{acl: "public-read"},
- %{success_action_status: "201"},
- ["starts-with", "$Content-Type", mimetype],
- %{"x-amz-credential": credential},
- %{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
- %{"x-amz-date": date}
- ]
- }
- |> Poison.encode!
- |> Base.encode64
- end
- defp credential(date) do
- credential(aws_config[:access_key_id], date)
- end
- defp credential(key, date) do
- key <> "/" <> date <> "/" <> region() <> "/" <> @service <> "/" <> @aws_request
- end
- makeMultiPart : UploadSignatureModel -> File -> Http.Body
- makeMultiPart uploadSignature file =
- Http.multipartBody
- [ Http.stringPart "key" uploadSignature.key
- , Http.stringPart "acl" uploadSignature.acl
- , Http.stringPart "success_action_status" "201"
- , Http.stringPart "Content-Type" uploadSignature.content_type
- , Http.stringPart "X-Amz-Credential" uploadSignature.credential
- , Http.stringPart "X-Amz-Algorithm" "AWS4-HMAC-SHA256"
- , Http.stringPart "Policy" uploadSignature.policy
- , Http.stringPart "Signature" uploadSignature.signature
- , Http.stringPart "AWSAccessKeyId" uploadSignature.aws_access_key_id
- , Http.filePart "file" file
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement