Is it possible to gzip and upload this string to Amazon S3 without ever being written to disk?

前端 未结 2 993
北荒
北荒 2021-02-06 00:38

I know this is probably possible using Streams, but I wasn\'t sure the correct syntax.

I would like to pass a string to the Save method and have it gzip the string and u

2条回答
  •  死守一世寂寞
    2021-02-06 01:14

    I would use something like the following:

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    GZipOuputStream gzipOut = new GZipOutputStream(byteOut);
    // write your stuff
    byte[] bites = byteOut.toByteArray();
    //write the bites to the amazon stream
    

    You are writing the zipped values out to the byte stream, then taking the byte values, you can write those to your other stream. You can also wrap the stream to the amazon site (i.e. the output stream from the http connection or something similar) and avoid the whole ByteArrayOutputStream.


    Edit: I noticed your last sentence - bleah. You can take the bytes you created, create a ByteArrayInputStream with them, and then pass that in as an input stream:

    ByteArrayInputStream byteInStream = new ByteArrayInputStream(bites);
    

    It should read from the input stream to the output stream, if I am understanding what you are describing correctly. Otherwise, you can simply write to the output stream.

提交回复
热议问题