-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveToS3.py
78 lines (60 loc) · 2.43 KB
/
moveToS3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import logging
import boto3
from botocore import UNSIGNED
from botocore.config import Config
from botocore.exceptions import ClientError
from datetime import datetime
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then same as file_name
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
def main():
"""Exercise upload_file()"""
# Set these values before running the program
bucket_name = 'dardendifferentialmodeloutput'
now = datetime.now()
# object_name = now.strftime("%d/%m/%Y %H:%M:%S") +" output.txt"
object_name = now.strftime("%d-%m-%Y %H:%M:%S") + " output.txt"
file_name = 'output1.txt'
stats_bucket_name = "nk-cournot-stats"
stats_file_name = now.strftime("%d-%m-%Y %H:%M:%S") + " stats.txt"
logging.info("uploading stats file name " + str(stats_file_name))
stats_file = "stats.txt"
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s')
# Upload raw output file
response = upload_file(file_name, bucket_name, object_name)
if response:
logging.info('Raw output file was uploaded')
# Upload stats file
stats_response = upload_file(stats_file, stats_bucket_name, stats_file_name)
if stats_response:
logging.info('Statistics file was uploaded')
if __name__ == '__main__':
main()