getObject from aws s3 got gibberish - javascript

in our project we have 1 service that uploads a local csv to s3, and I am working on another service which downloads csv that's uploaded using nodejs. I got gibberish in the content as i try to print the string of content (small csv).
code :
s3.getObject( {
Bucket: mybucket,
Key:mykey
}, function(err,data){
console.log(data.Body.toString()) ;
})
I have checked the data object that in the callback and found the type for buffer of data is unit8Array. I guess that's the cause. Does anyone know how I can control the type of buffer to be String? or is it something that needs to be changed during upload?

This is solved on the file upload side which is written in java. it was using AmazonS3EncryptionClientBuilder instead of AmazonS3ClientBuilder. as a result data coming back when downloading is encrypted that's why its gibberish. so it was not caused by buffer type

Related

Netsuite Suitescript Decode Base64

I'm doing Api integration with Suitescript 2.0. A data encoded with base64 is returned from the Api. Here I need to reach the data I want by decoding the base64 and saving the xml data returned as a .zip and unzip it.
The relevant data can be run in Notepad++ with Plugins > MIME Tools > Decode Base64, saved as zip and opened with unzip.
The script I'm working with is a shcedule script.
I tried the two methods mentioned to decode in Suite Answers.
1- From base64 to UTF_8 with N/encode module (Returned result is completely wrong for this problem)
2 - The solution in the link:
https://netsuite.custhelp.com/app/answers/detail/a_id/41271/kw/base64%20decode
(In this solution, when you save the returned data as zip, it gives an "Unexpected end of the archive" error when opening the zip.)
ArrayBuffer() and atob() are not available in Suitescript.
The thing I know will work is to proxy the call through a Lambda on some external system.
However if your data is already in base64 you might try just creating a file cabinet file and give it the base64 encoded value as its content. Netsuite already handles base64 for files so you might be overworking the issue. It doesn't sound like you are actually processing the xml if your end goal is to save it as a zip.
If this doesn't help see my comments regarding some clarifications you could add to your question.
require(["N/encode"], function(encode){
var txt = encode.convert({
string: "your Base64 string",
inputEncoding: encode.Encoding.BASE_64,
outputEncoding: encode.Encoding.UTF_8
});
}
SuiteScript example
All types of encode

Sending .wav file to backend

I am currently using RecorderJS and need to send a .wav file to the backend. the API is quite limited in documentation so I am struggling to figure out how to send the .wav file through my axios.post(...).
I am able to download the .wav file with
Recorder.download(theblob, 'audio.wav');
this downloads a .wav file which I can play through itunes so it is the right format. I now need to figure out how to save this in a variable in order to post it through axios. Also, what should i use for me headers, .. ect?
Looking for any kind of javascript solution to this. I just need to send the exact downloaded file to my backend. Thanks!
The download method stores the file somewhere on your disk. I believe javascript cannot traverse your computer's path and read files for security reasons. I'm not sure if recorder-js offers storing it in a variable "out of the box", so you may want to get that handled first.
For your second part of the question:
This should work for posting it to the back-end:
let data = new FormData();
data.append('wavfile', file, file.name);
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
axios.post('/api/recorderfiles', data, config)

Creating a pdf file from mpdf string output?

I'm using mPDF server side to create a pdf file. It works okay if I output the file to the server, however, I would like to return a string back to the client and build a pdf file from it which I can then use like any normal file from a file input.
server side, the (simplified) code is
$output_dest = 'S';
$content = $mpdf->Output($post_data->fileName, $output_dest);
//$mpdf->Output($post_data->fileName, 'F'); //just to check that the output should be correct
$response->setContent($content);
and client side i've tried using Blobs to create a file
var fileObj = new Blob([offerString], {type : 'application/pdf'});
but there are 2 problems. First, the blob, when sent to the server, doesn't have the required name. Secondly, the pdf file created (using window.saveAs to save the blob) is blank. It has the correct number of pages and author information, but it's completely blank.
If I use mPDF's file output, the resulting file is correct, so the problem must lie somewhere within the string->blob process.
Edit: The solution is to create the Blob not straight from the string but from an arrayBuffer. I've created the arrayBuffer using the solution suggested in another answer here

How to save a file from a URL in Parse.com using Javascript

I know the syntax for saving files in Parse.com (https://www.parse.com/docs/js_guide#files-classes), but now I have a URL instead of a local file - http://graph.facebook.com/{FacebookID}/picture, how can I save it in Parse? Thanks!
You can't create/save a Parse File with the contents of a remote file, only data you have locally (like a base 64 encoded string, or a byte array, etc.)
See the Parse File (JavaScript) docs here: https://parse.com/docs/js_guide#files

FileReader.readAsDataUrl and m4a Files

I have a piece of JavaScript code that reads a m4a file as an ArrayBuffer and pulls out the metadata (title, artist etc.). I am trying to unit test it by using the data url of the file (a base64 string) to create a file blob which I can then parse. I have to do this in order to have the unit test run without any user interaction. I have done this successfully using mp3 files: I use the copy the output of FileReader.readAsDataUrl as a hard coded string in my test class and it works as expected. However when I try and do the same with a m4a file it does not work. Specifically the ArrayBuffer created from the data URL is not the same length as when the ArrayBuffer is created directly. Does anyone know why this might be?

Categories