Download from S3 bucket

Dharanesh
Posts: 3
Joined: Thu Mar 28, 2024 5:43 am

Download from S3 bucket

Postby Dharanesh » Thu Apr 11, 2024 10:26 am

I've already written the esp self OTA code, and it functions well.
I now have to read and download a different file from Amazon S3 in order to flash it onto an AVR MCU that is attached to a UART. The locations of both firmwares are the same.

I am able to make the connection. I can't read this, however.

The code
  1.  
  2.         int content_length = esp_http_client_get_content_length(client);
  3.         ESP_LOGI("avr", "Content length: %d", content_length);
  4.         if (content_length < 0)
  5.         {
  6.             ESP_LOGE("avr", "Failed to get content length");
  7.         }
  8.         int buffer_size = snprintf(NULL, 0, "%d", content_length);
  9.         ESP_LOGI("avr", "Buffer size: %d", buffer_size);
  10.  
  11.         char *buffer = (char *)malloc(content_length);
  12.         if (buffer == NULL)
  13.         {
  14.             ESP_LOGE("avr", "Failed to allocate memory for buffer");
  15.         }
  16.         else
  17.         {
  18.             // Convert the content_length to a string and store it in the buffer
  19.             snprintf(buffer, buffer_size + 1, "%d", content_length+1);
  20.             ESP_LOGI("avr", "Buffer: %s", buffer);
  21.         }
  22.  
  23.         ESP_LOGI("avr","block 1");
  24.         int data_read_total = 0;
  25.         while (data_read_total <= content_length)
  26.         {
  27.             int data_read = esp_http_client_read(client, buffer, content_length);
  28.             // int data_read = esp_http_client_read(client, buffer + data_read_total, content_length - data_read_total);
  29.             ESP_LOGI("avr", "Data read: %d", data_read);
  30.             if (data_read <= 0)
  31.             {
  32.                 ESP_LOGE("avr", "Failed to read data");
  33.                 // break; // Exit the loop if reading fails
  34.             }
  35.             data_read_total += data_read;
  36.         }
  37.         buffer[content_length+1] = '\0'; // Null-terminate the buffer
  38.         printf("Buffer: %s\n", buffer);
  39.  
  40.         ESP_LOGI("avr", "Data read: %d", data_read_total);
  41.  
  42.         free(buffer);
The Log
  1. I (69399) avr: HTTP_EVENT_ON_HEADER, key=Server, value=AmazonS3
  2. I (69399) avr: HTTP_EVENT_ON_HEADER, key=Content-Length, value=27258
  3. I (69889) avr: HTTP_EVENT_ON_FINISH
  4. I (69889) avr: Content length: 27258
  5. I (69899) avr: Buffer size: 5
  6. I (69899) avr: Buffer: 27259
  7. I (69899) avr: block 1
  8. I (69909) avr: Data read: 0
  9. E (69909) avr: Failed to read data
  10. Buffer: 27259
  11. I (69909) avr: Data read: 0
I have to read the bin file and load it into Spiff. After that, I have to read the contents of Spiff and transmit it to AVR, page by page.
Last edited by Dharanesh on Mon Sep 02, 2024 5:27 am, edited 1 time in total.

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Download from S3 bucket

Postby ESP_Sprite » Fri Apr 12, 2024 2:19 am

Code: Select all

int buffer_size = snprintf(NULL, 0, "%d", content_length);
Can you explain this line?

Dharanesh
Posts: 3
Joined: Thu Mar 28, 2024 5:43 am

Re: Download from S3 bucket

Postby Dharanesh » Fri Apr 12, 2024 3:34 am

ESP_Sprite wrote:
Fri Apr 12, 2024 2:19 am

Code: Select all

int buffer_size = snprintf(NULL, 0, "%d", content_length);
Can you explain this line?
While the buffer's argument is char in esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len), the content_length is assigned to the buffer with that size since esp_http_client_get_content_length(client); returns int.


based on the content_length while allocating the buffer

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Download from S3 bucket

Postby ESP_Sprite » Fri Apr 12, 2024 5:11 am

Dharanesh wrote:
Fri Apr 12, 2024 3:34 am
While the buffer's argument is char in esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len), the content_length is assigned to the buffer with that size since esp_http_client_get_content_length(client); returns int.

based on the content_length while allocating the buffer
You're not doing an assignment, you're doing a snprintf. Why? (Also, you need to be clearer. 'is assigned to the buffer with that size' doesn't describe anything your C code does.)

Who is online

Users browsing this forum: Bing [Bot], username and 69 guests