I am trying to create a feedback button for my app. It basically just writes to a text file but it currently replaces the existing text file with the new feedback string. So basically I need to get the current text file and concatenate the text inside that text file with the new feedback so it doesn't overwrite anything.
Problem is whenever I get this text file it gives me what appears to be a Uint8Array
object:
The last line of these logs is the String to be added to the text file. Right above that is the object which was returned from the call to aws. As you can see it is some object and not a string. So in my .txt file it is uploading a [object Object]. The second log leads me to believe that the object is a Uint8Array because of the Body.
My code looks something like this:
function uploadLog(message, key) {
var moment = {key: key};
var deferred = $q.defer();
awsServices.getObject(key).then(function(data) {
console.log("NEW MESSAGE");
console.log(data);
data = new TextDecoder("utf-8").decode(data.Body);
// data = Utf8ArrayToStr(data.Body);
console.log(data);
console.log(message);
newMessage = message.toString() + '\r\n\r\n' + data;
var blob = new Blob([newMessage.toString()], {type: "text"});
var file = new File([blob], key);
awsServices.upload(file, moment.key, moment).then(function() {
deferred.resolve();
}, function(error) {
console.log("UPLOAD LOG REJECT");
deferred.reject();
});
}, function(error) {
console.log("ERROR");
console.log(error);
deferred.reject();
});
return deferred.promise;
};
I am basically just getting the text file from AWS, reading it, concatating the new string onto the file and then reuploading it.
Now I have tried a variety of functions to convert a Uint8Array to a String such as this one:
https://ourcodeworld.com/articles/read/164/how-to-convert-an-uint8array-to-string-in-javascript
and this one:
Conversion between UTF-8 ArrayBuffer and String
Niether seems to work(Still prints as an [object Object] and I have also tried TextDecoder.
Has anyone ever updated text or a log file in AWS S3? How did you do it? I have a slight suspicious that this file is not a Uint8Array file otherwise these conversion methods would have worked.
EDIT:
awsServices.getObject(key).then(function(data) {
console.log("UPLOAD LOG");
console.log(typeof(data)); //object
console.log(data); //*Screenshot above
console.log(data instanceof Blob); //false
console.log(data instanceof ReadableStream); //false
console.log(data instanceof Object); //true
data = uintToString(data.Body);
console.log(typeof(data)); //string
console.log(data); //[object Object]
newMessage = message.toString() + '\r\n\r\n' + data;
var blob = new Blob([newMessage.toString()], {type: "text"});
var file = new File([blob], key);
awsServices.upload(file, moment.key, moment).then(function() {
deferred.resolve();
}, function(error) {
console.log("UPLOAD LOG REJECT");
deferred.reject();
});
}, function(error) {