0

I'm passing an array of array to a java method and I need to add that data to a new file (which will be loaded into an s3 bucket)

How do I do this? I haven't been able to find an example of this

Also, I'm sure "object" is not the correct data type this attribute should be. Array doesn't seem to be the correct one.

Java method -

public void uploadStreamToS3Bucket(String[][] locations) {
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withRegion(String.valueOf(awsRegion))
                .build();

        String fileName = connectionRequestRepository.findStream() +".json";
        String bucketName = "downloadable-cases";
        File locationData = new File(?????)   // Convert locations attribute to a file and load it to putObject
        s3Client.putObject(new PutObjectRequest(bucketName, fileName, locationData));
    } catch (AmazonServiceException ex) {
        System.out.println("Error: " + ex.getMessage());
    }
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Tip
  • 19
  • 6

1 Answers1

2

You're trying to use PutObjectRequest(String,String,File) but you don't have a file. So you can either:

The later is better as you save the intermediate step.

As for how to write an object to a stream you may ask: Check this How can I convert an Object to Inputstream

Bear in mind to read it you have to use the same format.

It might be worth to think about what kind of format you want to save your information, because it might be needed to be read for another program, or maybe by another human directly from the bucket and there might be other formats / serializers that area easy to read (if you write JSON for instance) or more efficient (if you use another serializer that takes less space).

As for the type of array of array you can use the [][] syntax. For instance an array of array of Strings would be:

String [][] arrayOfStringArrays;

I hope this helps.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • In this case, I have to write the object to a file, but I can't find a way to do that correctly. – Tip Sep 16 '21 at 21:05
  • You have to save it to a file locally and then send it to S3? If you just want to write to a file use `Files.write(Path, byte[], OpenOptions...)` https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html#write(java.nio.file.Path,byte[],java.nio.file.OpenOption...) – OscarRyz Sep 16 '21 at 22:10