Peggy
Peggy

Reputation: 143

Python - How to upload files created today in a folder to S3

I have a folder called myfolder containing multiple files with filename as follows,

ID001_2017-04-15.csv, ID002_2017-04-15.csv, ID001_2017-04-16.csv, ID002_2017-04-16.csv, 
ID001_2017-04-17.csv, ID002_2017-04-17.csv, ID001_2017-04-18.csv, ID002_2017-04-18.csv

The date on the filename is the date the file created. For example, file ID001_2017-04-17.csvis created on 2017-04-17. Here's how I uploaded all the files in a folder to Amazon S3,

import boto3

def upload_files(path):
    session = boto3.Session(
              aws_access_key_id = 'this is my access key',
              aws_secret_access_key = 'this is my secret key',
              region_name = 'this is my region'
              )
    s3 = session.resource('s3')
    bucket = s3.Bucket('this is my bucket')

    for subdir, dirs, files in os.walk(path):
        for file in files:
            full_path = os.path.join(subdir, file)
            with open(full_path, 'rb') as data:
                bucket.put_object(Key = full_path[len(path) + 1:], Body = data)

if __name__ == "__main__":
    upload_files('path to myfolder') ## Replace this with your folder directory

My question is can I only upload files that are created today to Amazon S3?

Upvotes: 2

Views: 1826

Answers (1)

dogoncouch
dogoncouch

Reputation: 249

This will check if a file was created today:

import os.path
import datetime.datetime

# Create a datetime object for right now:
now = datetime.datetime.now()
# Create a datetime object for the file timestamp:
ctime = os.path.getctime('example.txt')
filetime = datetime.datetime.fromtimestamp(ctime)

# Check if they're the same day:
if filetime.year == now.year and filetime.month == now.month and filetime.day = now.day:
    print('File was created today')

If you put something like that in your for file in files: loop, you should be able to isolate the files created today.

Upvotes: 1

Related Questions