Google OAuth2.0 + Lambda + S3 Authorization - How to refer to a file from S3?

365 Views Asked by At

I am attempting to use the authentication from google, however I have doubts on how I can use in the method:

GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
    .createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));

The file MyProject-1234.json is stored on a S3 bucket and this is currently running inside a lambda, how can I use this file on the authentication? I am not sure if I should be sending the path and how, or if I should be doing something else.

2

There are 2 best solutions below

1
On BEST ANSWER

Here's how you can pull the file from S3 and use it.

In short, the getFileFromS3(...) method will return a File object that you can use to create FileInputStream.

public class S3FileTest implements RequestStreamHandler {

    private LambdaLogger logger;

    @Override
    public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        logger = context.getLogger();

        String bucketName = "==== S3 BUCKET NAME ====";
        String fileName = "==== S3 FILE NAME ====";

        File localFile = getFileFromS3(context, bucketName, fileName);

        if(localFile == null) {
            // handle error
            // return ....
        }

        // use the file
        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(localFile))
                .createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
        
        // do more
        // ...
    }

    private File getFileFromS3(Context context, String bucketName, String fileName) {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();

        // s3 client
        if (s3Client == null) {
            logger.log("S3 Client is null - can't continue!");
            return null;
        }

        // s3 bucket - make sure it exist
        if (!s3Client.doesBucketExistV2(bucketName)) {
            logger.log("S3 Bucket does not exists - can't continue!");
            return null;
        }


        File localFile = null;

        try {
            localFile = File.createTempFile(fileName, "");

            // get S3Object
            S3Object s3Object = s3Client.getObject(bucketName, fileName);

            // get stream from S3Object
            InputStream inputStream = s3Object.getObjectContent();

            // write S3Object stream into a temp file
            Files.copy(inputStream, localFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

            return localFile;
        } catch (Exception e) {
            logger.log("Failed to get file from S3: " + e.toString());
            return null;
        }
    }
}
0
On

You need to use the AWS SDK for Java to download the file from S3 to the Lambda function's local file system first. You can't use FileInputStream to open an S3 object, you can only use that to open a local file system object.