Search code examples
javafilefile-iofilewriterbufferedwriter

How to avoid saving a file on hard drive?


Each time I run the following code, the file is saved on a hard drive. However, I want it to be saved only in the Object Storage container.

OSClient os = OSFactory.builder()
                .endpoint("...")
                .credentials("...","...")
                .tenantName("...")
                .authenticate();

        String containerName = "MyImgs";
        String objectName = "test.jpg";

BufferedWriter output = null;
        try {
            File f = new File(objectName);
            output = new BufferedWriter(new FileWriter(f));
            output.write(text);
            String etag = os.objectStorage().objects().put(containerName, 
                                                           objectName, 
                                                           Payloads.create(f));
        } catch ( IOException e ) {
            e.printStackTrace();
        }

UPDATE: I am using this API.


Solution

  • Looking at the Javadoc for Payloads it has a method which takes in InputStream. To read an String as an InputStream you can do

    Payloads.create(new ByteArrayInputStream(text.getBytes());
    

    This will avoid the need to create a file just so you have something to read.