'Upload file at pre-signed URL using HttpPut method contains extra unnecessary part

I have pre-signed the URL. And my task is to upload a CSV file at a pre-signed URL. I have written below code

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.File;
public class Test {
    /**
     * Uploading file at pre-signed URL
     *
     * @throws IOException
     */
    private void uploadFileToAWSS3(String preSignedUrl) throws IOException {
        File file = new File("/Users/vmagadum/temp/test.csv");
        HttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(
                        RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()
                ).build();
        HttpPut put = new HttpPut(preSignedUrl);
        HttpEntity entity = MultipartEntityBuilder.create()
                .addPart("file", new FileBody(file))
                .build();
        put.setEntity(entity);
        put.setHeader("Content-Type","text/csv");

        HttpResponse response = httpClient.execute(put);

        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("File uploaded successfully at destination.");
        } else {
            System.out.println("Error occurred while uploading file.");
        }
    }
}

With the above code, I am able to upload the file at the destination but when I download that file then I can see an unnecessary part was also added.

Structure of File I want to Upload

File I want to Upload

The file I am getting after downloading from S3

File I am getting after download

How can I ignore this extra header part that was added to the file?



Solution 1:[1]

That is amazing @jindra-lacko! I took the liberty of checking the code above by creating a polar view map of Antarctica (holy grial of maps ;)), and works amazingly!:

library(sf)
library(giscoR) # for the countries dataset only
library(ggplot2)

# Test of https://stackoverflow.com/a/70756593/7877917

# projection string used for the polygons & ocean background
crs_string <- "+proj=ortho +lat_0=-90 +lon_0=0"

# background for the globe - center buffered by earth diameter
ocean <- st_point(x = c(0, 0)) %>%
  st_buffer(dist = 6371000) %>%
  st_sfc(crs = crs_string)

# country polygons, cut to size
world <- gisco_countries %>%
  st_intersection(ocean %>% st_transform(4326)) %>% # select visible area only
  st_transform(crs = crs_string) # reproject to ortho

# one of the visible ones red (don't really matter which one :)
world$fill_color <- ifelse(world$ISO3_CODE == "DEU", "interesting", "dull")

# background for the globe - center buffered by earth diameter
ocean <- st_point(x = c(0, 0)) %>%
  st_buffer(dist = 6371000) %>%
  st_sfc(crs = crs_string)

# now the action!
ggplot(data = world) +
  geom_sf(data = ocean, fill = "aliceblue", color = "black") + # background first
  geom_sf(aes(fill = fill_color), lwd = .1) + # now land over the oceans
  scale_fill_manual(
    values = c(
      "interesting" = "red",
      "dull" = "lightyellow"
    ),
    guide = "none"
  ) +
  theme_void() +
  labs(
    title = "Polar View: Antarctica",
    caption = "Based on Jindra Lacko:\nhttps://stackoverflow.com/a/70756593/7877917"
  )

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dieghernan