Google OAuth2.0 + Lambda + S3 授权 - 如何从 S3 引用文件?
Google OAuth2.0 + Lambda + S3 Authorization - How to refer to a file from S3?
我正在尝试使用来自 google 的身份验证,但是我对如何在方法中使用有疑问:
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
文件 MyProject-1234.json 存储在 S3 存储桶中,目前 运行 在 lambda 中,我如何在身份验证中使用此文件?我不确定我是否应该发送路径以及如何发送,或者我是否应该做其他事情。
您需要先使用 Java 的 AWS SDK 将文件从 S3 下载到 Lambda 函数的本地文件系统。您不能使用 FileInputStream
打开 S3 对象,只能使用它打开本地文件系统对象。
以下是从 S3 中提取文件并使用它的方法。
简而言之,getFileFromS3(...)
方法将 return 一个 File
对象,您可以使用该对象创建 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;
}
}
}
我正在尝试使用来自 google 的身份验证,但是我对如何在方法中使用有疑问:
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN));
文件 MyProject-1234.json 存储在 S3 存储桶中,目前 运行 在 lambda 中,我如何在身份验证中使用此文件?我不确定我是否应该发送路径以及如何发送,或者我是否应该做其他事情。
您需要先使用 Java 的 AWS SDK 将文件从 S3 下载到 Lambda 函数的本地文件系统。您不能使用 FileInputStream
打开 S3 对象,只能使用它打开本地文件系统对象。
以下是从 S3 中提取文件并使用它的方法。
简而言之,getFileFromS3(...)
方法将 return 一个 File
对象,您可以使用该对象创建 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;
}
}
}