1

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 Uint8Arrayobject:

enter image description here

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) {
6
  • Use data.Body.toString('ascii'). Commented Sep 17, 2017 at 0:08
  • I tried that one already Commented Sep 17, 2017 at 0:16
  • It should decode data.Body to a string according to the specified character encoding (you could use ascii or utf-8, for example). Why doesn't that work for you? Commented Sep 18, 2017 at 14:18
  • I have no clue. It logs out as [object Object] still and when I load it into AWS and open the text file all I see if [object Object]. Maybe its not an 'ascii' string? However, I also tried utf-8. Commented Sep 18, 2017 at 14:41
  • Does the text file in S3 have metadata: Content-Type = text/plain? Commented Sep 18, 2017 at 15:29

1 Answer 1

2

Ensure that the S3 object metadata has the correct content type: set Content-Type = text/plain.

Then use data.Body.toString('ascii'), or some other encoding such as utf-8.

Sign up to request clarification or add additional context in comments.

4 Comments

This didn't work actually. It is still coming up as [object Object]
What type is data.Body? In my case it's a Buffer, and the toString() method on Buffer returns a string. I believe that data.Body can be one of Buffer, Typed Array, Blob, String, or ReadableStream.
Well when I log out typeof(data) it comes out a object. So I'm not sure. Doing instanceof I figured out it is not a Blob or ReadableStream. I got an error saying Buffer and TypedArray is undefined so I didn't do an instanceof on those. I will update my code to reflect these log outputs.
Make sure you have up to date JavaScript/node.js.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.