dhatim / fastexcel

Generate and read big Excel files quickly
Other
647 stars 116 forks source link

Is it possible to read excel files from S3 Bucket #330

Open luv2manish opened 10 months ago

luv2manish commented 10 months ago

im trying to read excel files kept on S3 Bucket , somehow its not working

meiMingle commented 8 months ago

If you provide more useful information, maybe I can take a look at this issue sometime.Not everyone is willing to spend time figuring out what S3 Bucket is.

Adverte commented 7 months ago

obviously it is possible

arnaldop commented 4 months ago

This is totally possible. Just get an InputStream from AWS libraries and use it.

Get InputStream

public InputStream getS3FileInputStream(@NonNull final String key,@NonNull final String bucket) {
   log.debug("Reading file with key {} from S3 bucket {}", key, bucket);

   final GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);

   return new S3InputStream(getAmazonS3()
       .getObject(getObjectRequest)
       .getObjectContent());
}

Helper Class

@RequiredArgsConstructor
@Getter
@EqualsAndHashCode(callSuper = true)
public static class S3InputStream extends InputStream {
   private final InputStream is;

   @Override
   public int read() throws IOException {
       return is.read();
   }

   @Override
   public void close() throws IOException {
      try {
        IOUtils.drainInputStream(is);
      } catch (final Exception e) {
        // ignore
      }
      is.close();
   }
}

Use InputStream

try (
    final InputStream inputStream = userFileService.getInputStream(userFile)
) {
    doSomethingWithInputStream(inputStream)
} catch (final IOException e) {
    // ...
}

Open Workbook and Sheet

public void doSomethingWithInputStream(@NonNull final InputStream inputStream)
   try(
       final Workbook workbook=new ReadableWorkbook(inputStream)
   ){
       final Stream<Sheet> sheetStream=workbook.getSheets();

       // ...
   } catch(final IOException e){
       // ...
}