
Creator II
2024-09-13
10:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Upload file to s3 using presigned post url
Hi,
I found the java routine below for uploading file using presigned post URL :
I have downloaded the package httpcomponents-client-5.3.1-bin.tar.gz for uploading the jar below :
I uploaded the .jar in the project :
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
1 Solution
Accepted Solutions

Creator II
2024-09-16
05:58 AM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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);
}
}
}
300 Views
1 Reply

Creator II
2024-09-16
05:58 AM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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);
}
}
}
301 Views
