shadowlegend

shadowlegend

How to provide x-amz-content-sha256 to ex_aws_s3 presigned_url

hello, I would like to receive pre-computed sha256 hash of file upload from client and generate presigned url for client to upload the file to s3.

here is the code to generate the presigned_url

size = 123
mime_type = "image/jpg"
storage_class = "STANDARD_IA"
key = "some/path/0723faba-72b5-45bf-9373-ad37dbcf6a80.jpg"
hash = "output:{openssl dgst -sha256 /path/to/test/file.jpg}"

:s3
|> ExAws.Config.new()
|> ExAws.S3.presigned_url(
    :put,
    bucket_name(),
    key,
    [
      expires_in: @upload_expiry,
      query_params: [
        {"x-amz-acl", "private"},
        {"x-amz-storage-class", storage_class},
        {"x-amz-server-side-encryption", "AES256"}
      ],
      headers: [
        {"content-length", size},
        {"content-type", mime_type},
        {"x-amz-content-sha256", hash}
      ]
    ]
  )

I took the url generated from the above code and upload give me this error

<?xml version="1.0" encoding="UTF-8"?>
<Error>
	<Code>SignatureDoesNotMatch</Code>
	<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
	<AWSAccessKeyId>AKXXXXXXXXXXXXXXX</AWSAccessKeyId>
	<StringToSign>AWS4-HMAC-SHA256
20220118T095403Z
20220118/ap-southeast-1/s3/aws4_request
abc123</StringToSign>
	<SignatureProvided>abc456</SignatureProvided>
	<StringToSignBytes>41 57 XX XX</StringToSignBytes>
	<CanonicalRequest>PUT
some/path/0723faba-72b5-45bf-9373-ad37dbcf6a80.jpg
X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=AKXXXXXXXXXXXXXXX%2F20220118%2Fap-southeast-1%2Fs3%2Faws4_request&amp;X-Amz-Date=20220118T095403Z&amp;X-Amz-Expires=16&amp;X-Amz-SignedHeaders=content-length%3Bcontent-type%3Bhost%3Bx-amz-content-sha256&amp;x-amz-acl=private&amp;x-amz-server-side-encryption=AES256&amp;x-amz-storage-class=STANDARD_IA
content-length:123
content-type:image/jpg
host:s3.ap-southeast-1.amazonaws.com
x-amz-content-sha256:

content-length;content-type;host;x-amz-content-sha256
UNSIGNED-PAYLOAD</CanonicalRequest>
	<CanonicalRequestBytes>50 55 XX XX</CanonicalRequestBytes>
	<RequestId>9DXXXXXXXXXX</RequestId>
	<HostId>DZ7XXXXXX</HostId>
</Error>

When I remove the x-amz-content-sha256 from headers, the request upload the file as expected, and I could test around providing wrong content-type or content-length so the request would fail(as expected) meaning the presigned_url does work when providing headers value.

First Post!

mruoss

mruoss

Are you sure the content of the header in the request is exactly the same you create the signature for? No extra whitespace characters or different encoding?

Looking at the canonical request it seems like the value is just two newlines…

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement