YD

Hello Community,

I would like to build a report which can upload and download files (pdf, xls) from the Amazon S3 data storage.

I can already realize the uploading part by the following code:

    this.uploadFile = function(path, inputStream, fileName, userProperties) {

        

        var method = jString("PUT");

        var resource = jString(path + "/" + fileName);        

        var urlString = jString("http://" + headerHost + path + "/" + fileName);

        var authAWS = this.getAuthAWS4String(method, resource, userProperties);

        var url = new java.net.URL(urlString);

        try {

            var httpConn = url.openConnection();

        } catch (e) {            

            throw "Error uploading document to S3!\nCould not open connection to " + urlString;

        }

        

        httpConn.setDoOutput(true);

        httpConn.setRequestMethod(method);

        httpConn.setRequestProperty("host", headerHost);    

        httpConn.setRequestProperty("x-amz-date", getFormattedDateInUTC(currentDate,"yyyyMMdd'T'HHmmss'Z'"));    

        httpConn.setRequestProperty("authorization",authAWS);

        httpConn.setRequestProperty("x-amz-content-sha256",payloadHash);        

    

        if (userProperties) {

            var keys = Object.keys(userProperties);

            for(var i = 0; i < keys.length; i++){

                var key = keys[i];

                httpConn.setRequestProperty(key, userProperties[key] );

            }

        }

                    

        var url = new java.net.URL(urlString);

        try {    

            httpConn.connect();

        } catch (e) {

            throw "Error uploading document to S3!\nCould not connect to " + urlString;

        }

        

        var outputStream = httpConn.getOutputStream();

        

        var buffer = new Packages.java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, BUFFER_SIZE); //byte[] buffer = new byte[BUFFER_SIZE];

        var bytesRead = -1;

        while ((bytesRead = inputStream.read(buffer)) != -1) {

            outputStream.write(buffer, 0, bytesRead);

        }

        outputStream.close();

        inputStream.close();

        

        httpConn.disconnect();

        if (httpConn.getResponseCode()!=200)  {

            throw "Error uploading document to S3!\nResponse code: " + httpConn.getResponseCode()

                        + "\nResponse message: " + getErrorResponse(httpConn);  

        }

        return urlString;

    }

 

However, for the downloading part I am not sure how to do it. I can get a bytearray from the S3 storage and how can I convert this byte array into the file I wanted?

Thanks in advance!

Featured achievement

Rookie
Say hello to the ARIS Community! Personalize your community experience by following forums or tags, liking a post or uploading a profile picture.
Recent Unlocks
  • Profile picture for user frankweise
  • Profile picture for user Henrik Buckler
  • Profile picture for user UffeK
  • SS
  • MZ
  • PacMan

Leaderboard

|
icon-arrow-down icon-arrow-cerulean-left icon-arrow-cerulean-right icon-arrow-down icon-arrow-left icon-arrow-right icon-arrow icon-back icon-close icon-comments icon-correct-answer icon-tick icon-download icon-facebook icon-flag icon-google-plus icon-hamburger icon-in icon-info icon-instagram icon-login-true icon-login icon-mail-notification icon-mail icon-mortarboard icon-newsletter icon-notification icon-pinterest icon-plus icon-rss icon-search icon-share icon-shield icon-snapchat icon-star icon-tutorials icon-twitter icon-universities icon-videos icon-views icon-whatsapp icon-xing icon-youtube icon-jobs icon-heart icon-heart2 aris-express bpm-glossary help-intro help-design Process_Mining_Icon help-publishing help-administration help-dashboarding help-archive help-risk icon-knowledge icon-question icon-events icon-message icon-more icon-pencil forum-icon icon-lock