Zenko Machinebox.io tagbox integration - check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# Yannick Guillerm - Sales Engineer Scality | |
# yannick.guillerm@scality.com | |
# Zenko MachineBox.ai TagBox integration CHECK | |
# Description: uploads a file to Zenko S3. Run Machinebox.io TagBox CHECK. Populates the S3 object metadata with Machinebox.io metadata tags. | |
# 05/25/18 | |
import requests | |
import sys, getopt | |
import argparse | |
import os | |
import shutil | |
import ntpath | |
import json | |
from pprint import pprint | |
import subprocess | |
import shlex | |
def main(argv): | |
filepath = "" | |
# Parse arguments. File full path and op (upload/download) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--file', help='f help') | |
parser.add_argument('--endpointurl', help='endpointurl help') | |
parser.add_argument('--bucket', help='bucket help') | |
parser.add_argument('--mdmode', help='bucket help') | |
args = vars(parser.parse_args()) | |
metadatamode = "none" | |
filepath = args['file'] | |
filename = ntpath.basename(filepath) | |
zenkoendpoint = args['endpointurl'] | |
zenkobucket = args['bucket'] | |
metadatamode = args['mdmode'] | |
print metadatamode | |
if metadatamode != "metadata" and metadatamode != "tags": | |
print "Undefined metadata mode - Must be \"metadata\" or \"tags\"" | |
exit(0) | |
print filepath | |
print filename | |
# Upload file | |
print "Uploading file to Zenko ..." | |
command = 'aws --endpoint-url https://' + zenkoendpoint + ':443 s3api put-object --bucket ' + zenkobucket + ' --key ' + filename + ' --body ' + filepath | |
print command | |
os.system(command) | |
# Grant public read access | |
print "Granting public access ..." | |
command = 'aws --endpoint-url https://' + zenkoendpoint + ':443 s3api put-object-acl --bucket ' + zenkobucket + ' --key ' + filename + ' --acl public-read' | |
print command | |
os.system(command) | |
# Analyze file with machinebox | |
print "MachineBox analyzing file ..." | |
command = 'curl -H \'Content-Type: application/json\' -d \'{"url":"https://' + zenkoendpoint + '/' + zenkobucket + '/' + filename + '"}\' http://localhost:8080/tagbox/check' | |
print command | |
result = subprocess.check_output(command, shell=True) | |
print result | |
json_res = json.loads(result) | |
print json_res["tags"] | |
json_str = json.dumps(json_res) | |
if metadatamode == "tags": | |
print "Metadatamode is \"tags\"" | |
tagset_str = 'TagSet=[' | |
if 'tags' in json_res: | |
if len(json_res["tags"]) != 0: | |
i=0 | |
while i<len(json_res["tags"]): | |
print json_res["tags"][i]["tag"] | |
print json_res["tags"][i]["confidence"] | |
#Command syntax: aws s3api put-bucket-tagging --bucket bucketname --tagging 'TagSet=[{Key=organization,Value=sales}]' | |
tagset_str = tagset_str + '{Key=' + str(json_res["tags"][i]["tag"]) + ',Value=' + str(json_res["tags"][i]["confidence"]) + '}' | |
i=i+1 | |
if 'custom_tags' in json_res: | |
if len(json_res["custom_tags"]) != 0: | |
j=0 | |
while j<len(json_res["custom_tags"]): | |
print json_res["custom_tags"][j]["tag"] | |
print json_res["custom_tags"][j]["confidence"] | |
#Command syntax: aws s3api put-bucket-tagging --bucket bucketname --tagging 'TagSet=[{Key=organization,Value=sales}]' | |
tagset_str = tagset_str + '{Key=' + str(json_res["custom_tags"][j]["tag"]) + ',Value=' + str(json_res["custom_tags"][j]["confidence"]) + '}' | |
j=j+1 | |
if 'tags' in json_res or 'custom_tags' in json_res: | |
tagset_str = tagset_str.replace("}{","},{") | |
tagset_str = tagset_str + ']' | |
command = 'aws --endpoint-url https://' + zenkoendpoint + ':443 s3api put-object-tagging --bucket ' + zenkobucket + ' --key ' + filename + ' --tagging ' + '\'' + tagset_str + '\'' | |
print command | |
os.system(command) | |
elif metadatamode == "metadata": | |
print "Metadatamode is \"metadata\"" | |
mdset_str = "--metadata \"" | |
if 'tags' in json_res: | |
if len(json_res["tags"]) != 0: | |
i=0 | |
while i<len(json_res["tags"]): | |
print json_res["tags"][i]["tag"] | |
print json_res["tags"][i]["confidence"] | |
mdset_str = mdset_str + str(json_res["tags"][i]["tag"]) + '=' + str(json_res["tags"][i]["confidence"]) + ',' | |
i=i+1 | |
if 'custom_tags' in json_res: | |
if len(json_res["custom_tags"]) != 0: | |
j=0 | |
while j<len(json_res["custom_tags"]): | |
print json_res["custom_tags"][j]["tag"] | |
print json_res["custom_tags"][j]["confidence"] | |
mdset_str = mdset_str + str(json_res["custom_tags"][j]["tag"]) + '=' + str(json_res["custom_tags"][j]["confidence"]) + ',' | |
j=j+1 | |
if 'tags' in json_res or 'custom_tags' in json_res: | |
mdset_str = mdset_str + "\"" | |
mdset_str = mdset_str.replace(",\"","\"") | |
print mdset_str | |
command = 'aws --endpoint-url https://' + zenkoendpoint + ':443 s3api put-object --bucket ' + zenkobucket + ' --key ' + filename + " " + mdset_str | |
print command | |
os.system(command) | |
else: | |
print "Undefined metadata mode - Must be \"metadata\" or \"tags\"" | |
exit(0) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it as follows:
python /Users/yannickguillerm/Desktop/TechMarketing/Zenko/zenkocheck.py --f /Users/yannickguillerm/Desktop/TechMarketing/Zenko/TestFiles/Y-Wing-Test2.jpeg --endpointurl c5afccae-78c0-11e8-a153-0242ac110002.sandbox.zenko.io --bucket zenko-local --mdmode tags