2017-04-25 3 views
0

파이썬은 -, 나는 다음과 같이 파일 이름을 가진 여러 파일을 포함 <code>myfolder</code>라는 폴더가 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에 만들어집니다. 여기

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 

내 질문에 난 단지 아마존 S3에 오늘 생성 된 파일을 업로드 할 수 있습니다, 나는 아마존 S3에 폴더의 모든 파일을 업로드하는 방법가요? 파일이 오늘을 만든 경우

+0

http://stackoverflow.com/questions/5141437/filtering-os-walk-dirs-and-files - 오늘 날짜를 기준으로 필터링하십시오. – stdunbar

+1

로컬 디렉토리에서 S3으로 파일을 동기화하려는 경우 [aws s3 sync (AWS s3 sync)]가있는 [AWS Command-Line Interface (CLI)] (http://aws.amazon.com/cli/) '명령. 자신의 코드를 작성하는 것보다 훨씬 쉽습니다. –

+0

@JohnRotenstein 감사합니다. 예, 로컬 디렉토리의 파일을 S3에 동기화하려고합니다. CLI를 사용하여 오늘까지 생성 된 파일을 S3에 동기화 할 수 있습니까? – Peggy

답변

0

이 확인합니다 : 당신이 당신의 for file in files: 루프에서 그런 일을 넣으면

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') 

, 당신은 오늘 만든 파일을 분리 할 수 ​​있어야한다.

관련 문제