3
3
import json , copy
4
4
import os
5
5
6
- def send_s3 (filepath ,token ,production = False ):
7
-
6
+
7
+ def send_s3 (filepath , token , production = False ):
8
+
8
9
if production == True :
9
10
s3surl = "https://data.caltech.edu/tindfiles/sign_s3/"
10
11
chkurl = "https://data.caltech.edu/tindfiles/md5_s3"
11
12
else :
12
13
s3surl = "https://cd-sandbox.tind.io/tindfiles/sign_s3/"
13
14
chkurl = "https://cd-sandbox.tind.io/tindfiles/md5_s3"
14
15
15
- headers = { ' Authorization' : ' Bearer %s' % token }
16
+ headers = {" Authorization" : " Bearer %s" % token }
16
17
17
18
c = session ()
18
19
19
20
print (s3surl )
20
21
print (headers )
21
- response = c .get (s3surl ,headers = headers )
22
+ response = c .get (s3surl , headers = headers )
22
23
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
+ )
47
52
48
53
c = session ()
49
- response = c .post (url ,files = form , headers = s3headers )
54
+ response = c .post (url , files = form , headers = s3headers )
50
55
print (response )
51
- if ( response .text ) :
56
+ if response .text :
52
57
raise Exception (response .text )
53
58
54
- print (chkurl + '/' + bucket + '/' + key + '/' )
59
+ print (chkurl + "/" + bucket + "/" + key + "/" )
55
60
print (headers )
56
- response = c .get (chkurl + '/' + bucket + '/' + key + '/' , headers = headers )
61
+ response = c .get (chkurl + "/" + bucket + "/" + key + "/" , headers = headers )
57
62
print (response )
58
63
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 }
60
67
61
- fileinfo = { "url" : key ,\
62
- "filename" : filename ,\
63
- "md5" : md5 ,"size" : size }
68
+ return fileinfo
64
69
65
- return (fileinfo )
66
70
67
- def caltechdata_write (metadata ,token ,files = [],production = False ):
71
+ def caltechdata_write (metadata , token , files = [], production = False ):
68
72
69
- #If files is a string - change to single value array
73
+ # If files is a string - change to single value array
70
74
if isinstance (files , str ) == True :
71
75
files = [files ]
72
76
73
- fileinfo = []
77
+ fileinfo = []
74
78
75
79
for f in files :
76
80
fileinfo .append (send_s3 (f , token , production ))
@@ -80,20 +84,18 @@ def caltechdata_write(metadata,token,files=[],production=False):
80
84
else :
81
85
url = "https://cd-sandbox.tind.io/submit/api/create/"
82
86
83
- headers = {
84
- 'Authorization' : 'Bearer %s' % token ,
85
- 'Content-type' : 'application/json'
86
- }
87
+ headers = {"Authorization" : "Bearer %s" % token , "Content-type" : "application/json" }
87
88
88
89
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
+ ]
94
96
95
- dat = json .dumps ({' record' : newdata })
97
+ dat = json .dumps ({" record" : newdata })
96
98
97
99
c = session ()
98
- response = c .post (url ,headers = headers ,data = dat )
100
+ response = c .post (url , headers = headers , data = dat )
99
101
return response .text
0 commit comments