3

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?

2
  • Not unless you use something like s3fuse to mount the s3 bucket. github.com/s3fs-fuse/s3fs-fuse Commented Jan 13, 2021 at 16:17
  • You might also want to consider versioning these objects if you are going to download/edit/upload. Commented Jan 14, 2021 at 0:48

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

Comments

3

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),
        }

 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.