Home > Back-end >  Are incoming files via AWS Transfer Family into S3 taggable?
Are incoming files via AWS Transfer Family into S3 taggable?

Time:11-16

At the moment I am facing a problem, that I can't determine if a file was PUT via the AWS Transfer Family or via the S3 GUI.

Is there any change to default tag files which are PUT on S3 via AWS Transfer Family?

Regards Ribase

CodePudding user response:

There is S3 object metadata described in the Transfer Family user guide for post upload processing, which indicates Transfer Family uploaded this.

One use case and application of using the metadata is when an SFTP user has an inbox and an outbox. For the inbox, objects are put by an SFTP client. For the outbox, objects are put by the post upload processing pipeline. If there is an S3 event notification, the downstream service on the processor side can do an S3 HeadObject call for the metadata, dismiss if it does not have the metadata, and only process incoming files.

You could also use Transfer Family managed workflows to apply a Tag step. An example of application of using the Tag step can be found in demo 1 of the AWS Transfer Family managed workflows demo video.

CodePudding user response:

Configure the S3 bucket where Transfer Family is writing the files to trigger a Lambda using an Event Notification.

Use this Boto3 code in the Lambda. It will tag the file with the principal that placed the file in S3. If it is the Transfer Familiy then it is the role that was assigned to Transfer Family to write the files to the bucket. If it is a user uploading the files via the Console then it will be that users role.

import boto3
import json
import urllib.parse

def lambda_handler(event, context):
       
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
    principal = event['Records'][0]['userIdentity']['principalId']
    
    try:
          
       s3 = boto3.client('s3')
          
       response = s3.put_object_tagging(
                     Bucket = bucket,
                     Key = key,
                     Tagging={
                         'TagSet': [
                             {
                                 'Key': 'Principal',
                                 'Value': str(principal)
                             },
                         ]
                     }
                 )
                 
    except Exception as e:
       print('Error {}.'.format(e))
  • Related