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!