0
我有一個名爲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文件?
看看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