-2

I am trying to add system time as timestamp to my s3 bucket folder names so that every time i run the code, it would create a separate folder with a different time stamp on s3. How do i achieve this ?

import json
import boto3    
s3 = boto3.resource('s3')
s3object = s3.Object('your-bucket-name', 'your_file.json')

s3object.put(
    Body=(bytes(json.dumps(json_data).encode('UTF-8')))
)
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
Shabari nath k
  • 920
  • 1
  • 10
  • 23
  • Trying to automate hourly pull of data and storing it in s3 folders, separated by different time stamps. Data pulled at 2pm, goes to folder_2pm. Data pulled at 3 pm goes to folder_3pm. – Shabari nath k Mar 15 '20 at 11:23
  • The timestamps will be prefixes for the key names of the object you put to the s3 bucket. – Marcin Mar 15 '20 at 11:40
  • please check https://stackoverflow.com/questions/1939743/amazon-s3-boto-how-to-create-a-folder – dassum Mar 15 '20 at 11:45

1 Answers1

2

You would use standard Python date functions to construct the folder name you want, then set that string as part of the S3 object's key. Something like this:

import json
import boto3    
from datetime import datetime

s3 = boto3.resource('s3')
prefix = 'folder_' + datetime.now().strftime("%I%p") + "/"
s3object = s3.Object('your-bucket-name', prefix + 'your_file.json')

s3object.put(
    Body=(bytes(json.dumps(json_data).encode('UTF-8')))
)
Mark B
  • 183,023
  • 24
  • 297
  • 295