DoorPiMedia.py
1.18 KB
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
#!/usr/bin/env python3
import sys, os, glob, time, json, argparse, twilio, boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key
#Grab Details from the config file
with open('config.json') as data_file:
config = json.load(data_file)
#Get the file name from CLI arguments
fileToUpLoadToS3 = ''
fileToUpLoadToS3 = sys.argv[1]
file = open(fileToUpLoadToS3, 'r+')
fileName = file.name
fileName = os.path.basename(fileName)
try:
s3_connection = S3Connection(config['AWSs3Key'],config['AWSs3Secret'])
except Exception as e:
print("Error connecting to S3: {0}".format(e))
try:
# Attempt to get the S3 bucket with our S3 connection.
bucket = s3_connection.get_bucket(config['AWSBucketName'])
except Exception as e:
# Error out if we're unable to locate the S3 bucket.
print("Error connecting to S3 bucket: {0}".format(e))
try:
# Create a new key using image_file_path as the key
key = Key(bucket)
key.key = 'DoorPi/'+fileName
key.set_contents_from_filename(fileToUpLoadToS3)
except Exception as e:
# Error out if we're unable to upload the image.
print("Error uploading file to S3: {0}".format(e))
print('Uploaded file to S3 '+fileName)