Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
luv2manish opened this issue Sep 11, 2023 · 3 comments
Open

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

luv2manish opened this issue Sep 11, 2023 · 3 comments

Comments

@luv2manish
Copy link

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

@meiMingle
Copy link
Collaborator

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
Copy link

Adverte commented Nov 23, 2023

obviously it is possible

@arnaldop
Copy link

arnaldop commented Mar 8, 2024

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){
       // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants