Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Fernandez
Creator II
Creator II

Upload file to s3 using presigned post url

Hi,

I found the java routine below for uploading file using presigned post URL :

Capture.PNG

I have downloaded the package httpcomponents-client-5.3.1-bin.tar.gz for uploading the jar below

Capture.PNG

I uploaded the .jar in the project : 

Capture.PNG

Do you know why I get the errors as you can see in the first picture ?

The import org.apache.http cannot be resolved

HttpClient cannot be resolved to a type

 

Thanks for your help

 

Labels (3)
1 Solution

Accepted Solutions
Fernandez
Creator II
Creator II
Author

Hi,

I found another script that is working : 

package routines;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class WebServices {
 
 
    public static void main(String[] args) {
        String presignedUrl = "YOUR_PRESIGNED_URL_HERE"; // Replace with your presigned URL
        String filePath = "path/to/your/file.txt"; // Replace with the path to your file
 
        try {
            uploadFile(presignedUrl, filePath);
            System.out.println("File uploaded successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void uploadFile(String presignedUrl, String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile()) {
            throw new IOException("File does not exist or is not a valid file.");
        }
 
        URL url = new URL(presignedUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/octet-stream");
        connection.setRequestProperty("Content-Length", String.valueOf(file.length()));
 
        try (FileInputStream fis = new FileInputStream(file);
             OutputStream os = connection.getOutputStream()) {
 
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
        }
 
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_CREATED) {
            throw new IOException("Failed to upload file. HTTP response code: " + responseCode);
        }
    }
}

View solution in original post

1 Reply
Fernandez
Creator II
Creator II
Author

Hi,

I found another script that is working : 

package routines;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class WebServices {
 
 
    public static void main(String[] args) {
        String presignedUrl = "YOUR_PRESIGNED_URL_HERE"; // Replace with your presigned URL
        String filePath = "path/to/your/file.txt"; // Replace with the path to your file
 
        try {
            uploadFile(presignedUrl, filePath);
            System.out.println("File uploaded successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void uploadFile(String presignedUrl, String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile()) {
            throw new IOException("File does not exist or is not a valid file.");
        }
 
        URL url = new URL(presignedUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/octet-stream");
        connection.setRequestProperty("Content-Length", String.valueOf(file.length()));
 
        try (FileInputStream fis = new FileInputStream(file);
             OutputStream os = connection.getOutputStream()) {
 
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
        }
 
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_CREATED) {
            throw new IOException("Failed to upload file. HTTP response code: " + responseCode);
        }
    }
}