How to Rename or Copy Files on AWS Lambda function?

后端 未结 1 1795
走了就别回头了
走了就别回头了 2021-01-19 13:35

I am a bit new to AWS / Lambda from the technical side so I have a scenario adn I wanted your help.

I have a file that drops daily, I only care about the file on th

相关标签:
1条回答
  • 2021-01-19 13:51

    Your Lambda function, in whatever language, will use S3-SDK to deal with files in S3 bucket(s). With S3, files (objects) cannot be rename, but what you can do is copy to another name (object key), and delete the old file.

    How to copy file on S3

    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    s3client.copyObject(sourceBucketName, sourceKey, 
                        destinationBucketName, destinationKey);
    

    http://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingJava.html

    How to delete file on S3

    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
            try {
                s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
            } catch (AmazonServiceException ase) {
                System.out.println("Caught an AmazonServiceException.");
                System.out.println("Error Message:    " + ase.getMessage());
                System.out.println("HTTP Status Code: " + ase.getStatusCode());
                System.out.println("AWS Error Code:   " + ase.getErrorCode());
                System.out.println("Error Type:       " + ase.getErrorType());
                System.out.println("Request ID:       " + ase.getRequestId());
            } catch (AmazonClientException ace) {
                System.out.println("Caught an AmazonClientException.");
                System.out.println("Error Message: " + ace.getMessage());
            }
    

    http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingOneObjectUsingJava.html

    What you probably have to do is to list objects in the bucket within particular folder, then find your target files (files that are modified/created on particular date/time?), then do whatever you need to do with it, if not copy possibly involve reading up the file, edit it then re-upload to S3.

    0 讨论(0)
提交回复
热议问题