33import json , copy
44import os
55
6- def send_s3 (filepath ,token ,production = False ):
7-
6+
7+ def send_s3 (filepath , token , production = False ):
8+
89 if production == True :
910 s3surl = "https://data.caltech.edu/tindfiles/sign_s3/"
1011 chkurl = "https://data.caltech.edu/tindfiles/md5_s3"
1112 else :
1213 s3surl = "https://cd-sandbox.tind.io/tindfiles/sign_s3/"
1314 chkurl = "https://cd-sandbox.tind.io/tindfiles/md5_s3"
1415
15- headers = { ' Authorization' : ' Bearer %s' % token }
16+ headers = {" Authorization" : " Bearer %s" % token }
1617
1718 c = session ()
1819
1920 print (s3surl )
2021 print (headers )
21- response = c .get (s3surl ,headers = headers )
22+ response = c .get (s3surl , headers = headers )
2223 jresp = response .json ()
23- data = jresp ['data' ]
24-
25- bucket = jresp ['bucket' ]
26- key = data ['fields' ]['key' ]
27- policy = data ['fields' ]['policy' ]
28- aid = data ['fields' ]['AWSAccessKeyId' ]
29- signature = data ['fields' ]['signature' ]
30- url = data ['url' ]
31-
32- infile = open (filepath ,'rb' )
33- size = infile .seek (0 ,2 )
34- infile .seek (0 ,0 ) #reset at beginning
35-
36- s3headers = { 'Host' : bucket + '.s3.amazonaws.com' ,\
37- 'Date' : 'date' ,\
38- 'x-amz-acl' : 'public-read' ,\
39- 'Access-Control-Allow-Origin' : '*' }
40-
41- form = ( ( 'key' , key )
42- , ("acl" , "public-read" )
43- , ('AWSAccessKeyID' , aid )
44- , ('policy' , policy )
45- , ('signature' , signature )
46- , ('file' , infile ))
24+ data = jresp ["data" ]
25+
26+ bucket = jresp ["bucket" ]
27+ key = data ["fields" ]["key" ]
28+ policy = data ["fields" ]["policy" ]
29+ aid = data ["fields" ]["AWSAccessKeyId" ]
30+ signature = data ["fields" ]["signature" ]
31+ url = data ["url" ]
32+
33+ infile = open (filepath , "rb" )
34+ size = infile .seek (0 , 2 )
35+ infile .seek (0 , 0 ) # reset at beginning
36+
37+ s3headers = {
38+ "Host" : bucket + ".s3.amazonaws.com" ,
39+ "Date" : "date" ,
40+ "x-amz-acl" : "public-read" ,
41+ "Access-Control-Allow-Origin" : "*" ,
42+ }
43+
44+ form = (
45+ ("key" , key ),
46+ ("acl" , "public-read" ),
47+ ("AWSAccessKeyID" , aid ),
48+ ("policy" , policy ),
49+ ("signature" , signature ),
50+ ("file" , infile ),
51+ )
4752
4853 c = session ()
49- response = c .post (url ,files = form , headers = s3headers )
54+ response = c .post (url , files = form , headers = s3headers )
5055 print (response )
51- if ( response .text ) :
56+ if response .text :
5257 raise Exception (response .text )
5358
54- print (chkurl + '/' + bucket + '/' + key + '/' )
59+ print (chkurl + "/" + bucket + "/" + key + "/" )
5560 print (headers )
56- response = c .get (chkurl + '/' + bucket + '/' + key + '/' , headers = headers )
61+ response = c .get (chkurl + "/" + bucket + "/" + key + "/" , headers = headers )
5762 print (response )
5863 md5 = response .json ()["md5" ]
59- filename = filepath .split ('/' )[- 1 ]
64+ filename = filepath .split ("/" )[- 1 ]
65+
66+ fileinfo = {"url" : key , "filename" : filename , "md5" : md5 , "size" : size }
6067
61- fileinfo = { "url" : key ,\
62- "filename" : filename ,\
63- "md5" : md5 ,"size" : size }
68+ return fileinfo
6469
65- return (fileinfo )
6670
67- def caltechdata_write (metadata ,token ,files = [],production = False ):
71+ def caltechdata_write (metadata , token , files = [], production = False ):
6872
69- #If files is a string - change to single value array
73+ # If files is a string - change to single value array
7074 if isinstance (files , str ) == True :
7175 files = [files ]
7276
73- fileinfo = []
77+ fileinfo = []
7478
7579 for f in files :
7680 fileinfo .append (send_s3 (f , token , production ))
@@ -80,20 +84,18 @@ def caltechdata_write(metadata,token,files=[],production=False):
8084 else :
8185 url = "https://cd-sandbox.tind.io/submit/api/create/"
8286
83- headers = {
84- 'Authorization' : 'Bearer %s' % token ,
85- 'Content-type' : 'application/json'
86- }
87+ headers = {"Authorization" : "Bearer %s" % token , "Content-type" : "application/json" }
8788
8889 newdata = customize_schema .customize_schema (copy .deepcopy (metadata ))
89- newdata ['files' ] = fileinfo
90- if 'doi' not in newdata :
91- #We want tind to generate the identifier
92- newdata ['final_actions' ] = [{"type" :"create_doi" ,\
93- "parameters" :{"type" :"records" ,"field" :"doi" }}]
90+ newdata ["files" ] = fileinfo
91+ if "doi" not in newdata :
92+ # We want tind to generate the identifier
93+ newdata ["final_actions" ] = [
94+ {"type" : "create_doi" , "parameters" : {"type" : "records" , "field" : "doi" }}
95+ ]
9496
95- dat = json .dumps ({' record' : newdata })
97+ dat = json .dumps ({" record" : newdata })
9698
9799 c = session ()
98- response = c .post (url ,headers = headers ,data = dat )
100+ response = c .post (url , headers = headers , data = dat )
99101 return response .text
0 commit comments