使用Java将文件从一个AWS S3存储桶复制到另一个S3存储桶

时间:2020-08-25 23:58:15

标签: java amazon-web-services amazon-s3 aws-lambda

我正在尝试使用Java编写的lambda函数从一个存储桶中复制清单文件,修改其内容,并将修改后的文件粘贴到另一个S3存储桶中。 以下是详细方案。我知道lambda函数需要如何触发。我需要Java程序的帮助,该程序将执行第4点中编写的操作。

  1. 有2个AWS S3存储桶(我们将其称为Bucket_A和Bucket_B)

  2. Bucket_A \ Folder_A包含一个manifest.csv文件,其内容如下:CLIENT_NAME,CLIENT_ACCOUNT_NUMBER,PROJECT_NUMBER,PA_NAME,PA_EMAIL,TRANSMISSION_DATE,PRODUCT_TYPE,JOB_TYPE,NUMBER_OF_FILES,FILES XYZ,XYZ0001,1234567,XYZ,XYZ @ gmail.com,20200825,Q,Q,1,Input.bfc.enc.gz

  3. Bucket_B \ Folder_B将收到一个应触发lambda函数的.txt文件

  4. 底层Java程序需要- 一世。从Bucket_A \ FolderA复制manifest.csv文件 ii。将名为FILES的列的值(Input.bfc.enc.gz)修改为另一个名称(例如Input.bfc) iii。将修改后的文件粘贴到Bucket_B \ Folder_B

我是AWS和Java的新手。请让我知道是否需要提供有关上述情况的更多详细信息。到现在为止,下面的代码已被编写。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.event.S3EventNotification;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;

public class CopyS3Files implements RequestHandler < S3EventNotification,
Integer > {@Override
  public Integer handleRequest(S3EventNotification event, Context context) {
    LambdaLogger logger = context.getLogger();
    Map < String,
    String > MetadataMap; // = new HashMap<String, String>();
    int fileCopyFlag = 0;
    String currentBucketName = event.getRecords().get(0).getS3().getBucket().getName();
    String objectKey = event.getRecords().get(0).getS3().getObject().getKey();
    String env = System.getenv("runtime");
    logger.log("Environment ==>" + env);
    System.out.println("bucketName::" + currentBucketName);
    System.out.println("objectKey::" + objectKey);
    //        AmazonS3 s3 = getAWSClients.getS3(env);
    logger.log("Got S3 notification for : " + currentBucketName + "/" + objectKey + "\n");
    S3Object o = AmazonS3ClientBuilder.standard().build().getObject(currentBucketName, objectKey);
    S3ObjectInputStream s3is = o.getObjectContent();
    logger.log("Fetched S3 Object--" + objectKey);
    try {
      MetadataMap = readMetadata(s3is, logger);
      String inputBucketFromMetadata = MetadataMap.get("INPUT_BUCKET");
      fileCopyFlag = copyManifestFile(inputBucketFromMetadata, currentBucketName);
      if (fileCopyFlag == 1) logger.log("Manifest file copied to the BFC input bucket successfully");
      else logger.log("Manifest file could not be copied to the BFC input bucket");
    } catch(FileNotFoundException e1) {
      logger.log("File Not Found Exception " + e1);
      e1.printStackTrace();
    } catch(IOException e2) {
      logger.log("Input Output Exception " + e2);
    }
    return 1;
  }

  public int copyManifestFile(String inputBucketFromMetadata, String currentBucketName) {
    String sourceKey = "manifest.csv";
    String destinationKey = "manifest.csv";
    /*Need to write the logic to perform steps in point #4 here*/
    return 1;
  }

  public static Map < String,
  String > readMetadata(InputStream s3Md, LambdaLogger logger)
  throws FileNotFoundException,
  IOException {
    logger.log("Reading Metadata File.......................");
    File tempMetadata = File.createTempFile("Metadata", null);
    tempMetadata.deleteOnExit();
    FileOutputStream out = new FileOutputStream(tempMetadata);
    IOUtils.copy(s3Md, out);
    List < Map < String,
    String >> namefreq = null;
    try (BufferedReader in =new BufferedReader(
    new FileReader(tempMetadata))) {
      namefreq = in.lines().skip(1).map(line ->line.split("\\|")).map(line ->{
        Map < String,
        String > map = new HashMap < >();
        map.put("INPUT_BUCKET", line[0]);
        map.put("OUTPUT_BUCKET", line[1]);
        map.put("FILE_NAME", line[2]);
        map.put("CRYPT_SCHEME", line[3]);
        map.put("CRYPT_TYPE", line[4]);
        return map;
      }).collect(Collectors.toList());
    }
    logger.log("List of Map contains the contents of Metadata::" + namefreq);
    System.out.println("List of Map contains the contents of Metadata::" + namefreq);
    return namefreq.get(0);
  }
}

0 个答案:

没有答案