Python的 - 如何今天的文件夹中创建的文件上传到S3
问题描述:
我有一个名为myfolder
含有多个文件名作为文件夹下面,Python的 - 如何今天的文件夹中创建的文件上传到S3
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
在文件名中的日期是文件创建日期。例如,文件ID001_2017-04-17.csv
创建于2017-04-17。以下是我上传的所有文件的文件夹到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
我的问题是我只能上传,今天被创建到Amazon S3文件?
答
这会检查文件是否是今天发布:
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')
如果你把类似的东西在你的for file in files:
循环,你应该能够发展到今天所创建的文件隔离。
看看http://stackoverflow.com/questions/5141437/filtering-os-walk-dirs-and-files - 并在今天的日期过滤。 – stdunbar
如果您打算将本地目录中的文件同步到S3,则可以使用[AWS命令行界面(CLI)](http://aws.amazon.com/cli/),该文件具有aws s3同步'命令。比编写自己的代码容易得多。 –
@JohnRotenstein谢谢。是的,我想将本地目录中的文件同步到S3。是否可以仅使用CLI将今天生成的文件同步到S3? – Peggy