从Elm / Elixir AWS4-HMAC-SHA256将文件发布到S3

时间:2019-05-27 13:07:20

标签: amazon-s3 elixir elm

我正在尝试从Elm应用程序发布到S3。我有一个后端服务,可以生成发布帖子所需的签名,策略等。

我已经阅读了文档以及许多有关如何发布到S3以及需要使用AWS4-HMAC-SHA256的文章,但是,我的代码仍然因错误而失败。

使用Elixir代码创建签名

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

使用Elixir代码创建签名

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

Elixir代码创建凭据

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
        ]

很明显,我缺少了一些东西,但是我一生无法解决。

编辑:

用户得到的错误代码是:

    <Error> 
      <Code>InvalidRequest</Code> 
      <Message>The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.</Message> 
      <RequestId>0B1FCA2C05E910B1</RequestId> 
 <HostId>7ydiqVEEPu22aN+o1BJhAQDQbDXBodChOfHv7986R8ItnhQ5hv0/iETzakTH8gLVljjqKr3lIUg=</HostId> 
   </Error> 

0 个答案:

没有答案