2020# Try to load .env
2121try :
2222 from dotenv import load_dotenv
23+
2324 load_dotenv ()
2425except ImportError :
2526 pass
@@ -32,7 +33,9 @@ def _b2_cmd() -> list[str]:
3233 # b2 v1.3.8 installed in system python without a CLI entrypoint
3334 for py in ["/usr/bin/python3" , "python3" , "python" ]:
3435 if shutil .which (py ):
35- r = subprocess .run ([py , "-m" , "b2" , "version" ], capture_output = True , text = True )
36+ r = subprocess .run (
37+ [py , "-m" , "b2" , "version" ], capture_output = True , text = True
38+ )
3639 if r .returncode == 0 :
3740 return [py , "-m" , "b2" ]
3841 return ["b2" ] # let it fail with a clear error
@@ -48,7 +51,9 @@ def authorize_b2() -> bool:
4851
4952 result = subprocess .run (
5053 [* _b2_cmd (), "authorize-account" , key_id , app_key ],
51- capture_output = True , text = True , timeout = 30 ,
54+ capture_output = True ,
55+ text = True ,
56+ timeout = 30 ,
5257 )
5358 if result .returncode != 0 :
5459 print (f"b2 authorize-account failed: { result .stderr [:300 ]} " , file = sys .stderr )
@@ -65,7 +70,9 @@ def upload_file(local_path: Path, b2_path: str, duration: int = 604800) -> str |
6570
6671 result = subprocess .run (
6772 [* _b2_cmd (), "upload-file" , bucket , str (local_path ), b2_path ],
68- capture_output = True , text = True , timeout = 120 ,
73+ capture_output = True ,
74+ text = True ,
75+ timeout = 120 ,
6976 )
7077 if result .returncode != 0 :
7178 print (f"b2 upload-file failed: { result .stderr [:300 ]} " , file = sys .stderr )
@@ -75,17 +82,26 @@ def upload_file(local_path: Path, b2_path: str, duration: int = 604800) -> str |
7582 return _get_presigned_url (bucket , b2_path , duration )
7683
7784
78- def _get_presigned_url (
79- bucket : str , b2_path : str , duration : int = 604800
80- ) -> str | None :
85+ def _get_presigned_url (bucket : str , b2_path : str , duration : int = 604800 ) -> str | None :
8186 """Get a presigned download URL. Default duration: 7 days (604800s)."""
8287 result = subprocess .run (
83- [* _b2_cmd (), "get-download-url-with-auth" , "--duration" , str (duration ),
84- bucket , b2_path ],
85- capture_output = True , text = True , timeout = 30 ,
88+ [
89+ * _b2_cmd (),
90+ "get-download-url-with-auth" ,
91+ "--duration" ,
92+ str (duration ),
93+ bucket ,
94+ b2_path ,
95+ ],
96+ capture_output = True ,
97+ text = True ,
98+ timeout = 30 ,
8699 )
87100 if result .returncode != 0 :
88- print (f"b2 get-download-url-with-auth failed: { result .stderr [:300 ]} " , file = sys .stderr )
101+ print (
102+ f"b2 get-download-url-with-auth failed: { result .stderr [:300 ]} " ,
103+ file = sys .stderr ,
104+ )
89105 # Fallback to direct URL (will 401 on private buckets)
90106 return f"https://f005.backblazeb2.com/file/{ bucket } /{ b2_path } "
91107
@@ -115,7 +131,7 @@ def main():
115131
116132 b2_path = f"{ args .prefix } /{ local_path .name } "
117133
118- print (f "Authorizing B2..." , file = sys .stderr )
134+ print ("Authorizing B2..." , file = sys .stderr )
119135 if not authorize_b2 ():
120136 sys .exit (1 )
121137
0 commit comments