@@ -68,17 +68,35 @@ def doit(account_name, image_filename):
6868 # --form requireSignedURLs=
6969
7070 # here's examples using metadata and expiry.
71+
72+ # this is just simple metadata created to show it working - your code will be different
7173 metadata_values = {
7274 'source' : image_filename ,
7375 'size' : image_filesize ,
7476 }
75- files = {
76- ('metadata' , (None , json .dumps (metadata_values ))),
77- ('expiry' , (None , time_plus_one_hour_in_iso ))
78- }
77+
78+ # this code works with 2.14.2 in a simpler way
79+
80+ data = None
81+ files = None
82+ if CloudFlare .__version__ <= '2.14.2' :
83+ print ('Using %s version of Cloudflare python library - hence using neither data or files' )
84+ else :
85+ # with newer library than 2.17.0 you should be able to pass just the data version
86+ print ('Using %s version of Cloudflare python library - hence using %s' % (CloudFlare .__version__ , 'data' if CloudFlare .__version__ > '2.17.0' else 'files' ))
87+ if CloudFlare .__version__ > '2.17.0' :
88+ data = {
89+ 'metadata' : json .dumps (metadata_values ),
90+ 'expiry' : time_plus_one_hour_in_iso ,
91+ }
92+ else :
93+ files = {
94+ ('metadata' , (None , json .dumps (metadata_values ))),
95+ ('expiry' , (None , time_plus_one_hour_in_iso ))
96+ }
7997
8098 try :
81- r = cf .accounts .images .v2 .direct_upload .post (account_id , files = files )
99+ r = cf .accounts .images .v2 .direct_upload .post (account_id , data = data , files = files )
82100 except CloudFlare .exceptions .CloudFlareAPIError as e :
83101 exit ('%s: %d %s - api call failed' % ('/accounts/images/v2/direct_upload' , e , e ))
84102 print ('v2 new image post results' )
@@ -97,9 +115,14 @@ def doit(account_name, image_filename):
97115 except Exception as e :
98116 exit ('%s: %s - api call failed' % (image_url , e ))
99117
100- response_code = r .status_code
101- if response_code != 200 :
102- exit ('%s: HTTP Error %s' % (image_url , response_code ))
118+ image_fp .close ()
119+
120+ if r .status_code != 200 :
121+ if r .status_code == 403 :
122+ print ('403 means you need to enable images in your account' )
123+ if r .status_code == 403 :
124+ print ('415 means the file is a bad image format' )
125+ exit ('%s: HTTP Error %s' % (image_url , r .status_code ))
103126
104127 j = r .json ()
105128 if j ['success' ] == True :
@@ -108,8 +131,6 @@ def doit(account_name, image_filename):
108131 else :
109132 exit ('Error:\n errors: %s\n messages: %s' % (image_url , j ['errors' ], j ['messages' ]))
110133
111- image_fp .close ()
112-
113134 # list all images
114135 try :
115136 r = cf .accounts .images .v2 (account_id )
@@ -119,8 +140,11 @@ def doit(account_name, image_filename):
119140 print ('All account images:' )
120141 for img in r ['images' ]:
121142 print ('%s %s: %s %s %s' % ('>' if img ['id' ] == image_id else ' ' , img ['id' ], img ['uploaded' ], img ['filename' ], img ['variants' ][0 ]))
122- for k ,v in img ['meta' ].items ():
123- print (' %s = %s' % (k , v ))
143+ if 'meta' in img :
144+ for k ,v in img ['meta' ].items ():
145+ print (' %s = %s' % (k , v ))
146+ else :
147+ print (' - no meta data' )
124148
125149 # delete the image - this was just a test (comment this out if you end up using this code for uploads)
126150 try :
0 commit comments