vll1990
vll1990

Reputation: 381

How to edit S3 files with python

I am working with python and I need to check and edit the content of some files stored in S3.
I need to check if they have a char o string. In that case, I have to replace this char/string.

For example: I want to replace ; with . in following file File1.txt

This is an example;

After replace

File1.txt

This in an example.

Is there a way to do the replace without downloading the file?

Upvotes: 3

Views: 5415

Answers (2)

vll1990
vll1990

Reputation: 381

Thanks for the answers.
I need to perform this action in a lambda and this is the result:

    import boto3
    import json
    
    s3 = boto3.client('s3')
    
    def lambda_handler(event, context):
        file='test/data.csv'
        bucket = "my-bucket"
        response = s3.get_object(Bucket=bucket,Key=file )
        
        fileout = 'test/dout.txt'
        rout = s3.get_object(Bucket=bucket,Key=fileout )
    
        
        data = []
        it = response['Body'].iter_lines()
        
        for i, line in enumerate(it):
            # Do the modification here
            modification_in_line = line.decode('utf-8').xxxxxxx  # xxxxxxx is the action
            data.append(modification_in_line)
    
        
        r = s3.put_object(
            Body='\n'.join(data), Bucket=bucket, Key=fileout,)
        
        
        return {
            'statusCode': 200,
            'body': json.dumps(data),
        }

 

Upvotes: 3

John Rotenstein
John Rotenstein

Reputation: 269340

Objects in Amazon S3 are immutable (they cannot be changed).

I recommend that your code does the following:

  • Download the file from Amazon S3 to local disk
  • Perform edits on the local file
  • Upload the file to Amazon S3 with the same Key (filename)

Upvotes: 7

Related Questions