views:

29

answers:

0

Hello, sry for my English, i'm bad at it. My problem is: I generate signed Upload link to amazon

signedUrl = StorageConnection.getInstance().getS3Service().createSignedPutUrl(buck_name, KeyID, null, expiryDate);

And i want to upload at this from other program, zipping file at stream:

private void put(String link, InputStream stream) throws MalformedURLException, IOException {
        URL url = new URL(link);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setRequestMethod("PUT");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);
        con.connect();

        byte buffer[] = new byte[8192 * 8];
        OutputStream output = con.getOutputStream();

        ZipOutputStream zos = new ZipOutputStream(output);
        zos.putNextEntry(new ZipEntry("Zipped"));

        int read = 0;

       while ((read = stream.read(buffer, 0, buffer.length)) > 0) {
            zos.write(buffer, 0, read);
            zos.flush();
            output.flush();
            System.out.println(read + " free mem=" + Runtime.getRuntime().freeMemory());
        }
        zos.closeEntry();
        zos.close();

        output.close();

        int rc = con.getResponseCode();
  con.disconnect();
        if (rc == 201) {
        }
    }

if file size more then heap size, java trows exeption :

Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2786)
        at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
        at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61)
        at SQr_client.Request.put(Request.java:147)
        at SQr_client.Request.Upload(Request.java:263)
        at SQr_client.Request.GetLinkAndUpload(Request.java:240)
        at SQr_client.Request.run(Request.java:62)
        at java.lang.Thread.run(Thread.java:619)

with "small" files all ok. What i must do? Using Runtime is not good. Files can be 2gb size.

I tried with

con.setChunkedStreamingMode(8192);
 con.setRequestProperty("Content-type", "application/octet-stream");

but file does not appear in the s3 bucket.