-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload-s3.py
44 lines (41 loc) · 1.42 KB
/
upload-s3.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import datetime
import os
import sys
import atexit
import imghdr
from subprocess import call
from os.path import expanduser, exists, basename, getsize
from workflow import Workflow
def capture():
file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S.png')
if (sys.argv[1] != ""):
# Image path is expected if additional argument found and will be verified by Alfred file filter
file_path = sys.argv[1]
else:
# Get image from clipboard
file_path = os.path.join('/tmp', file_name)
atexit.register(lambda x: os.remove(x) if os.path.exists(x) else None, file_path)
save = call(['./pngpaste', file_path])
if save == 1:
# Image not found in clipboard
print ("No image found in clipboard")
sys.exit()
return file_path, file_name
def main(wf):
import boto3
file_path, file_name = capture()
bucket_name = os.getenv('bucket_name')
s3 = boto3.client(
's3',
aws_access_key_id=os.getenv('access_key'),
aws_secret_access_key=os.getenv('secret_key')
)
s3.upload_file(file_path, bucket_name, file_name, ExtraArgs={'ContentType': "image/png"})
output = "%s/%s" %(os.getenv('bucket_uri'), file_name)
print (output,end='')
if __name__ == '__main__':
wf = Workflow(libraries=['./lib'])
sys.exit(wf.run(main))