Need assistance for creating HTTP GET request on Amazon S3 Bucket

Hello everyone,

I am currently creating code that will send out a HTTP Get request to a S3 bucket. I have most of the code functional. I am able to connect to the AWS services and send out headers. However, I keep getting a 400 error (or some form of the 400 error) I believe that I have an issue with my headers and what I am sending out. I am currently using the ArduinoHttpClient library to handle the HTTP GET Requests.

Below is the copy of my code that is doing the GET requests. I have some values hidden for obvious security reasons

void updateConfigFileEthernet(String server)
{
    EthernetClient secondClient;

    bool connectionEstablished = false;
    int err = 0;
    int stringIndex = server.indexOf(",");
    URLKVPair keyValuePairs[10]; 

    String response[2];
    String baseURL;

    parseAmazonURL(server, baseURL, keyValuePairs);

    int sectionIndex = baseURL.indexOf("//", 8); // Need to find the second one
    response[0] = baseURL.substring(8, sectionIndex);
    response[1] = baseURL.substring(sectionIndex + 1, baseURL.length());

    for(auto printString : response){
        Serial.println("Return Values:");
        Serial.println(printString);
    }

    Serial.print("Base URL: ");
    Serial.println(baseURL);

    HttpClient http(secondClient, response[0]);

    http.beginRequest();
    http.get(response[1]);

    keyValuePairs[2].value.replace("%2F", "/");

    String creds = keyValuePairs[2].value;
    String algorithmAmazon = keyValuePairs[0].value;
    String amazonSignature = keyValuePairs[6].value;

    String authorizationHeader = "Authorization: " + algorithmAmazon + " Credential=" + creds + ", SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=" + amazonSignature;
    Serial.println("Auth Header:");

    Serial.println(authorizationHeader); 
    http.sendHeader(authorizationHeader); // The Authorization header is as follows: Authorization: AWS4-HMAC-SHA256 Credential=<credValue>/20240212/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=<Signature value>

    http.sendHeader(keyValuePairs[1].key, keyValuePairs[1].value); // This header is this value: X-Amz-Content-Sha256=UNSIGNED-PAYLOAD
    http.sendHeader(keyValuePairs[3].key, keyValuePairs[3].value); // This header is this value: X-Amz-Date=20240212T003504Z
    http.sendHeader(keyValuePairs[5].key, keyValuePairs[5].value); // This is the x-amz-security-token

    // for(auto kvPairValue : keyValuePairs){
    //     if(kvPairValue.key != ""){
    //         http.sendHeader(kvPairValue.key, kvPairValue.value);
    //         Serial.println("Sending Out:");
    //         Serial.print("Key: ");
    //         Serial.print(kvPairValue.key);
    //         Serial.print(", Value: ");
    //         Serial.println(kvPairValue.value);
    //     }
    // }

    http.endRequest();

    int statusCode = http.responseStatusCode();
    
    Serial.print("GET Status code: ");
    Serial.println(statusCode);
    
    if(statusCode == 200){
        //l,String getBody = http.responseBody();
        http.skipResponseHeaders();
        Serial.println("Connnection successful");

        if(SPIFFS.exists("/CONFIG.txt")){
            SPIFFS.remove("/CONFIG.txt");
        }
        
        testFile = SPIFFS.open("/CONFIG.txt", FILE_WRITE);
        while(http.connected() || http.available()){
            if(http.available()){
                char c = http.read();
                testFile.write(c);
                Serial.print(c);
            } else {
                delay(1);
            }
        }

        testFile.close();
        transmitString("rst");
        delay(10);
        ESP.restart();
    } else {
        String getBody = http.responseBody();
        Serial.println("Connection failed");
        PrintUDPPort("Config file update failed - Connection unsuccesssful");

        Serial.print("GET Response: ");
        Serial.println(getBody);
    }    
}

As a side note, I am currently using this site as a basis for my HTTP GET request structure. I do have a security token that I am sending.

I should note that my current error is a 400 error that states :“InvalidTokenThe provided token is malformed or otherwise invalid.”

Oops, forgot to post a link to the site that I am using to structure my HTTP GET Request

https://czak.pl/2015/09/15/s3-rest-api-with-curl.html

Ok so for the token, since I am parsing out the info from a URL parameter, I need to replace some %2 with the actual ASCII characters. Now I am getting another 403 error:

SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.