Download from S3 bucket

Dharanesh
Posts: 4
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

Code: Untitled.cpp Select all

 
int content_length = esp_http_client_get_content_length(client);
ESP_LOGI("avr", "Content length: %d", content_length);
if (content_length < 0)
{
ESP_LOGE("avr", "Failed to get content length");
}
int buffer_size = snprintf(NULL, 0, "%d", content_length);
ESP_LOGI("avr", "Buffer size: %d", buffer_size);

char *buffer = (char *)malloc(content_length);
if (buffer == NULL)
{
ESP_LOGE("avr", "Failed to allocate memory for buffer");
}
else
{
// Convert the content_length to a string and store it in the buffer
snprintf(buffer, buffer_size + 1, "%d", content_length+1);
ESP_LOGI("avr", "Buffer: %s", buffer);
}

ESP_LOGI("avr","block 1");
int data_read_total = 0;
while (data_read_total <= content_length)
{
int data_read = esp_http_client_read(client, buffer, content_length);
// int data_read = esp_http_client_read(client, buffer + data_read_total, content_length - data_read_total);
ESP_LOGI("avr", "Data read: %d", data_read);
if (data_read <= 0)
{
ESP_LOGE("avr", "Failed to read data");
// break; // Exit the loop if reading fails
}
data_read_total += data_read;
}
buffer[content_length+1] = '\0'; // Null-terminate the buffer
printf("Buffer: %s\n", buffer);

ESP_LOGI("avr", "Data read: %d", data_read_total);

free(buffer);
The Log

Code: Untitled.cpp Select all

I (69399) avr: HTTP_EVENT_ON_HEADER, key=Server, value=AmazonS3
I (69399) avr: HTTP_EVENT_ON_HEADER, key=Content-Length, value=27258
I (69889) avr: HTTP_EVENT_ON_FINISH
I (69889) avr: Content length: 27258
I (69899) avr: Buffer size: 5
I (69899) avr: Buffer: 27259
I (69899) avr: block 1
I (69909) avr: Data read: 0
E (69909) avr: Failed to read data
Buffer: 27259
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.

Sprite
Espressif staff
Espressif staff
Posts: 10650
Joined: Thu Nov 26, 2015 4:08 am

Re: Download from S3 bucket

Postby 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: 4
Joined: Thu Mar 28, 2024 5:43 am

Re: Download from S3 bucket

Postby Dharanesh » Fri Apr 12, 2024 3:34 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

Sprite
Espressif staff
Espressif staff
Posts: 10650
Joined: Thu Nov 26, 2015 4:08 am

Re: Download from S3 bucket

Postby Sprite » Fri Apr 12, 2024 5:11 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: PerplexityBot and 1 guest