Download a file from AWS S3 using raw MQL5

 

Hi everyone,

I wanted to ask for your help, as I've been trying to download a file from a presigned AWS S3 url using the WebRequest function, but with no success. I had successfully downloaded a file from another URL of a website I own without any problem, but when trying to do the same for the S3 url, with the same codebase, I get the 4002 error:  ERR_WRONG_INTERNAL_PARAMETER - Wrong parameter in the inner call of the client terminal function.

The base of the URL is correctly added into Tools -> Options -> Expert Advisors -> Allow WebRequest for the listed URL (https://s3.eu-west-1.amazonaws.com).

The presigned S3 URL always starts with: https://s3.eu-west-1.amazonaws.com, and it then follows with the Bucket name, the route to the file, and then the presigned authorization. Here's an example: https://s3.eu-west-1.amazonaws.com/bucket-name/folder/file_name.csv?response-content-disposition=attachment%3B%20filename%3D%22file_name.csv%22&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=TJGHYHEHFDS8DIKJ....

As you can see, the presigned URL is quite long, but it is already correctly encoded. Also, as it is presigned, the request doesn't need any headers for authentication. You would just copy and paste it to your browser, and the file would start downloading.

I suspect it might be an HTTPS thing, as the documentation says that WebRequest sends an HTTP request, and AWS might be expecting an HTTPS. But on the other side, the URL for the test file I downloaded from my own site is also HTTPS, and the WebRequest worked just fine.

Here's the code I'm using, that worked for my own HTTPS site, but throws the 4002 error for the S3 url:

bool download_file_from_s3(string _url)
  {
   //Example S3 url: https://s3.eu-west-1.amazonaws.com/...

   //Headers
   //string headers = "User-Agent: MetaTrader/5\r\n\r\n";
   string headers = "";

   // Reset the last error code before making the request
   ResetLastError();
   
   // Variables to store server response
   char response[];
   char data[];
   string response_headers;

   // Make the WebRequest
   int response_code = WebRequest("GET", _url, headers, 10000, data, response, response_headers);

   // Check if the WebRequest encountered an error
   if(response_code == -1)
     {
      int error_code = GetLastError();
      PrintFormat("Error in WebRequest. Error code = %d", error_code);
      return false;
     }
(...)
}                       

Does anyone know which might be the cause of the 4002 error?

Thank you in advance!

Best regards

Documentation on MQL5: Network Functions / WebRequest
Documentation on MQL5: Network Functions / WebRequest
  • www.mql5.com
The function sends an HTTP request to a specified server. The function has two versions: 1. Sending simple requests of type "key=value...