I got a 403 response code when I tried to upload a file using HttpsUrlConnection on the AWS bucket.
Please see my code:
String fileToUpload = "/data/data/YTI.tar.xz";
String uploadUrl = "https://mybucket.s3.amazonaws.com/";
Log.v(TAG, " fileToUpload: " + fileToUpload);
Log.v(TAG, " uploadUrl: " + uploadUrl);
FileInputStream fileInputStream = null;
HttpsURLConnection connection = null;
OutputStream outputStream = null;
try {
File file = new File(fileToUpload);
fileInputStream = new FileInputStream(file);
URL url = new URL(uploadUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(new TLSSocketFactory());
connection.setRequestMethod("PUT");
connection.setRequestProperty("x-amz-acl", "bucket-owner-full-control");
connection.setRequestProperty("Content-Type", " ");
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode((int) file.length());
outputStream = connection.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
Log.v(TAG, " bytesRead: ");
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
int responseCode = connection.getResponseCode();
Log.v(TAG, "getResponseMessage: " + connection.getResponseMessage());
if (responseCode == HttpsURLConnection.HTTP_OK) {
Log.v(TAG, "File uploaded successfully!");
} else {
Log.e(TAG, "File upload failed with response code: " + responseCode);
PrevaLog.e(ErrorCode.E_DIGI_UPLOAD_FAILED, "Upload failure of the DIGI report.");
}
} catch (Exception e) {
e.printStackTrace();
PrevaLog.e(ErrorCode.E_DIGI_UPLOAD_FAILED, "Upload failure of the DIGI report.");
}
However, when I tried it using curl it uploads successfully with 200 status code.
Here is my curl command:
curl -v --cacert /system/vendor/certs/trusted/cacert.pem -H "x-amz-acl: bucket-owner-full-control" --request PUT --upload-file YTI.tar.xz "https://mybucket.s3.amazonaws.com/"
Any help would be appreciated. Thanks.