Parikh
Parikh

Reputation: 51

How to Rename or Copy Files on AWS Lambda function?

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 the last day of the month. They all drop to the same bucket the file drops at 8 EST.

I then need to rename the file from the last day of the month to a static name, and copying it to a bucket lets say the file is called bill. I would want the previous file there called bill_september if we are in october.

So my thought is to have a cron job kick off a Lambda function every day at noon to move a file except for the last day of the month. the first day of the month at 8 AM I will have it kick off a lambda job at 5 AM to copy to new bucket.

So the questions are

  1. Does this make sense?
  2. Can I have lambda rename existing file to file+month?

I am always open to a better solution so please tell me if i am totally turned around

Upvotes: 5

Views: 5093

Answers (1)

Ken Ratanachai S.
Ken Ratanachai S.

Reputation: 3557

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.

Upvotes: 4

Related Questions