- Newest
- Most votes
- Most comments
You have control on which file name to use. If you want to use a fixed name, just use that in your S3 API.
Hello,
One possible approach is to append date time to your filename to generate a unique filename before upload in your lambda function. I'm not a Python or code expert but see if following helps.
from datetime import datetime
now = datetime.now() # current date and time
dt = now.strftime("%Y-%m-%d-%H-%M-%S") #store date time in string
file = "file-" + dt #append the name to a string to generate filename
You can test the code on a local machine first and see if it meets the requirement.
I want to give names like file1, file2,.....filen don't want to use other name else it will create many files everyday and create mess while consuming them
Sorry it won't work cause it will create multiple files every time which i don't want..how can i give unique name to the four files every time so that it will replace the destination files.
I'm assuming you are using the boto3 S3 Client.
The put_object
, upload_file
and related methods accept a Key
parameter so you can name the file in S3 whatever you'd like.
Relevant content
- Accepted Answerasked a year ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 6 months ago
Here is my code:
def lambda_handler(event, context):
six files getting uploaded so this code runs 6 times where to give name which should be changed every event.
Please help...!
You specify the filename in the
client.put_object(Bucket=bucket, Key=filename,Body=csv_buffer.getvalue())
line. Just changefilename
to the value you want. Currently you gave itfile1.csv
.Lookin at another answer I see that you want to use filenames such as file1, file2, etc. To make sure that every invocation uses a different N, you will need to persist it somewhere, e.g., DynamoDB, and use it in each invocation. You will also need to make sure to zero it every day.