@@ -37,9 +37,13 @@ def _get_auth_header(self, method: str, url: str, content_type: Optional[str] =
3737 # Generate Qiniu token for the request
3838 # For live streaming API, we use a simple token format
3939 token = self ._generate_qiniu_token (method , url , content_type , body )
40+ # token = generate_signature(method, url, body,self.access_key, self.secret_key)
4041 return {
4142 "Authorization" : f"Qiniu { token } "
4243 }
44+ return {
45+ "Authorization" : f"Qiniu ak:sk"
46+ }
4347
4448 def _build_bucket_url (self , bucket : str ) -> str :
4549 """Build S3-style bucket URL"""
@@ -82,15 +86,36 @@ async def create_bucket(self, bucket: str) -> Dict[str, Any]:
8286 Dict containing the response status and message
8387 """
8488 url = self ._build_bucket_url (bucket )
85- headers = self ._get_auth_header (method = "PUT" ,url = url )
86-
87- logger .info (f"Creating bucket: { bucket } at { url } " )
89+ data = {}
90+ bodyJson = json .dumps (data )
91+ auth_headers = self ._get_auth_header (method = "PUT" , url = url , content_type = "application/json" , body = bodyJson )
92+ headers = {"Content-Type" : "application/json" }
93+ # 如果有认证头,添加到headers中
94+ if auth_headers :
95+ headers .update (auth_headers )
96+
97+ # 打印 HTTP 请求信息
98+ print ("=== HTTP 请求信息 ===" )
99+ print (f"方法: PUT" )
100+ print (f"URL: { url } " )
101+ print ("请求头:" )
102+ for key , value in headers .items ():
103+ print (f" { key } : { value } " )
104+ print ("请求体: {}" )
105+ print ("===================" )
106+
107+ print (f"Creating bucket: { bucket } at { url } " )
88108
89109 async with aiohttp .ClientSession () as session :
90- async with session .put (url , headers = headers ) as response :
110+ async with session .put (url , headers = headers , data = bodyJson ) as response :
91111 status = response .status
92112 text = await response .text ()
93113
114+ print (f"=== HTTP 响应信息 ===" )
115+ print (f"状态码: { status } " )
116+ print (f"响应内容: { text } " )
117+ print ("==================" )
118+
94119 if status == 200 or status == 201 :
95120 logger .info (f"Successfully created bucket: { bucket } " )
96121 return {
@@ -122,12 +147,17 @@ async def create_stream(self, bucket: str, stream: str) -> Dict[str, Any]:
122147 Dict containing the response status and message
123148 """
124149 url = self ._build_stream_url (bucket , stream )
125- headers = self ._get_auth_header (method = "PUT" , url = url )
150+ data = {}
151+ bodyJson = json .dumps (data )
152+ headers = {
153+ ** self ._get_auth_header (method = "PUT" , url = url , content_type = "application/json" , body = bodyJson ),
154+ "Content-Type" : "application/json"
155+ }
126156
127157 logger .info (f"Creating stream: { stream } in bucket: { bucket } at { url } " )
128158
129159 async with aiohttp .ClientSession () as session :
130- async with session .put (url , headers = headers ) as response :
160+ async with session .put (url , headers = headers , data = bodyJson ) as response :
131161 status = response .status
132162 text = await response .text ()
133163
@@ -458,19 +488,18 @@ async def list_streams(self, bucket_id: str) -> Dict[str, Any]:
458488 def _generate_qiniu_token (self , method : str , url : str , content_type : Optional [str ] = None , body : Optional [str ] = None ) -> str :
459489 if not self .access_key or not self .secret_key :
460490 raise ValueError ("QINIU_ACCESS_KEY and QINIU_SECRET_KEY are required" )
461-
462491 # Parse the URL
463492 parsed = urlparse (url )
464493
465494 # 1. Add Method and Path
466- data = f"{ method } { parsed .path } "
495+ data = f"{ method } { parsed .path or '/' } "
467496
468497 # 2. Add Query if exists
469498 if parsed .query :
470499 data += f"?{ parsed .query } "
471500
472501 # 3. Add Host
473- data += f"\n Host: { parsed .netloc } "
502+ data += f"\n Host: { parsed .hostname } "
474503
475504 # 4. Add Content-Type if exists and not empty
476505 if content_type :
@@ -495,9 +524,41 @@ def _generate_qiniu_token(self, method: str, url: str, content_type: Optional[st
495524 sign = hmac .new (secret_bytes , data_bytes , hashlib .sha1 ).digest ()
496525
497526 # 8. URL-safe Base64 encode
498- encoded_sign = base64 .urlsafe_b64encode (sign ).decode ('utf-8' )
527+ encoded_pre = base64 .b64encode (sign ).decode ('utf-8' )
528+ encoded_sign = encoded_pre .replace ('+' , '-' ).replace ('/' , '_' )
499529
500530 # 9. Construct and return the Qiniu token
501531 qiniu_token = f"{ self .access_key } :{ encoded_sign } "
532+ return qiniu_token
533+
534+
535+
536+
537+
538+
539+ def generate_signature (method , url , body , ak , sk ):
540+ parsed_url = urlparse (url )
541+
542+ # 构建签名数据
543+ data = method + " " + parsed_url .path
544+
545+ if parsed_url .query :
546+ data += "?" + parsed_url .query
547+
548+ data += "\n Host: " + parsed_url .hostname
549+ data += "\n Content-Type: application/json"
550+
551+ if body :
552+ data += "\n \n " + body
553+ print (data )
554+ # 使用HMAC-SHA1进行签名
555+ hmac_sha1 = hmac .new (sk .encode ('utf-8' ), data .encode ('utf-8' ), hashlib .sha1 )
556+ hmac_result = hmac_sha1 .digest ()
557+
558+ sign = ak + ":" + base64_url_safe_encode (hmac_result )
559+ return sign
502560
503- return qiniu_token
561+ def base64_url_safe_encode (data ):
562+ encoded = base64 .b64encode (data ).decode ('utf-8' )
563+ encoded = encoded .replace ('+' , '-' ).replace ('/' , '_' )
564+ return encoded
0 commit comments