I'm currently working on a project in Android Studio that implements AWS S3. The use of S3 is to upload/store my local SQLite database file on it so that I can download and use it on command. I realize that this is by no means the most optimal way to be using databases with S3.
I'm able to upload and download files to and from my S3 bucket. But for some reason, the database file is corrupted according to logcat. The SQLite database file is stored in the device's database folder when it is downloaded.
Here's the implemented code:
public void uploadFile(String fileName) {
File exampleFile = new File(getApplicationContext().getDatabasePath("Login.db").getPath());
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
fileName,
exampleFile,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
public void downloadFile() {
Amplify.Storage.downloadFile(
"Login.db",
new File(getApplicationContext().getDatabasePath("Login.db") + ""),
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
}
I'm looking for some insight on this matter. I'm just not sure what is causing the file corruption. I was thinking it could be the file path but I believe that is being navigated correctly.
I suspect that your issue is that the file is in fact corrupted and probably due to you using
writer.append("Example file contents");and saving a file that contains just that.You could perhaps add code to do some checks before uploading and after downloading a file.
The following is a snippet from code that does as such (in this case checking that an asset is a valid sqlite file, it's perhaps a little over the top) :-
The main thing is checking the header.