[{"data":1,"prerenderedAt":33},["ShallowReactive",2],{"snippet-lambda-function-convert-s3-storageclass":3},{"id":4,"title":5,"contributors":6,"description":8,"extension":9,"gitHub":10,"introBox":13,"languages":17,"meta":19,"resources":20,"services":21,"snippets":23,"stem":28,"tags":29,"type":31,"__hash__":32},"snippets\u002Fsnippets\u002Flambda-function-convert-s3-storageclass.json","Lambda function to convert S3 storage class",[7],"content\u002Fcontributors\u002Farun-thangavel.json","This function will help to convert your existing S3 storage class to the defined destination storage class.","json",{"template":11},{"repoURL":12},"https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fserverless-snippets\u002Ftree\u002Fmain\u002Flambda-function-convert-s3-storageclass",{"headline":14,"text":15},"How it works",[16],"This snipped creates a Lambda function in Python helps to convert your existing S3 storage class to the defined destination storage class.",[18],"Python",{},null,[22],"lambda-event-functions",[24],{"title":25,"language":26,"code":27},"Copy the code into Lambda","py","import json\nimport boto3\nimport urllib.parse\n\ns3 = boto3.client('s3')\n\n# Below Lambda function will convert any storage class to defined destination storage class\n\n# Specify target object bucket and key to convert its current storage class\nbucket = 'bucketName'\nkey = urllib.parse.unquote_plus('folder1\u002Ffolder2\u002Ffilename.jpg', encoding='utf-8')\n\ndest_StorageClass = 'INTELLIGENT_TIERING'\n# other possible values -> DEEP_ARCHIVE, GLACIER, GLACIER_IR, ONEZONE_IA, OUTPOSTS, REDUCED_REDUNDANCY, STANDARD, STANDARD_IA,\n\ndef lambda_handler(event, context):\n\n    # Get the object bucket and key from the S3 event to convert its current storage class\n    # bucket = event['Records'][0]['s3']['bucket']['name']\n    # key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')\n\n    copy_source = {\n        'Bucket': bucket,\n        'Key': key\n    }\n\n    try:\n        s3.copy(\n            copy_source, bucket, key,\n            ExtraArgs={\n                'StorageClass': 'INTELLIGENT_TIERING',\n                'MetadataDirective': 'COPY'\n            }\n        )\n        return {\n            'statusCode': 200,\n            'body': json.dumps(f'Success: object \"{key}\" from bucket \"{bucket}\" is converted to destination storage class \"{dest_StorageClass}\" ')\n        }\n    except Exception as e:\n        print(e)\n        print(f'Error: while copying the object \"{key}\" from bucket \"{bucket}\" to destination storage class \"{dest_StorageClass}\" ')\n        raise e\n","snippets\u002Flambda-function-convert-s3-storageclass",[30],"Amazon S3","Integration","MsrMT-cxSTe1mRsisKQmHsdAWuFlmZGOYfwrKsSR9qY",1792578864424]