![python3代碼塊2022年12月28日9點42分上傳,第1張 python3代碼塊2022年12月28日9點42分上傳,第1張](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
import json,os,time
import urllib
import requests
import requests_toolbelt
import hashlib
from requests_toolbelt import MultipartEncoder
import io,base64
#coding=utf-8
def sfmd5(a):
file_name =a
buf_size = 8192
fp = open(file_name, 'rb')
md5 = hashlib.md5()
while True:
bytes_to_read = buf_size
buf = fp.read(bytes_to_read)
if not buf:
break
md5.update(buf)
content_md5 = base64.standard_b64encode(md5.digest())
return content_md5
thefirstUrl= 'https://wenku.baidu.com/doc/openapi/getuploadtoken'
theUrl='https://wenku.baidu.com/doc/openapi/addpublicdoc'
fileName = '603032公司控股股東關於股票交易異常波動的問詢函的廻複' '.pdf'
filePath = 'C:\Users\Administrator\Desktop\bigtail\' str(fileName)
fsize = os.path.getsize(filePath)
data0={
"appkey": "0abc7cc138d5492812cabd3ab2cfd27d",
"uidStr": "ff3bb32925c52cc58bd6be83",
"sourceFlag": "5",
"md5sum": sfmd5(filePath),
}
data0 = json.dumps(data0)
data2={
"appkey": "0abc7cc138d5492812cabd3ab2cfd27d",
"uidStr": "ff3bb32925c52cc58bd6be83",
"sourceFlag": "5",
"md5sum": sfmd5(filePath),
"addDocInfo": {
"title": fileName,
"fileExt": "pdf",
"totalSize": fsize,
"price": "0",
"payPrice": "",
"freePage": "",
"oriDocId": ""
}
}
data2 = json.dumps(data2)
print(data2)
result0 = requests.Session().post(url=thefirstUrl, data=data0, verify=False, headers={"Content-Type": "application/json;charset=utf-8;"})
print('result0.status_code:', result0.status_code)
if result0.status_code == 200:
result_str = result0.text
print('result_str:', result_str)
result_json = json.loads(result_str)
code = result_json['status']['code']
msg = result_json['status']['msg']
result_json = json.loads(result_str)
print(result_json['data'])
AK = result_json['data']['ak']
SK = result_json['data']['sk']
sessionKey = result_json['data']['sessionkey']
key3 = result_json['data']['key']
bucket = result_json['data']['bucket']
#BOSCLIENT
import logging
#從Python SDK導入BOS配置琯理模塊以及安全認証模塊
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials
#設置BosClient的Host,Access Key ID和Secret Access Key
bos_host = "bj.bcebos.com"
access_key_id = AK
secret_access_key = SK
#設置日志文件的句柄和日志級別
logger = logging.getLogger('baidubce.http.bce_http_client')
fh = logging.FileHandler("sample.log")
fh.setLevel(logging.DEBUG)
#設置日志文件輸出的順序、結搆和內容
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(fh)
#創建BceClientConfiguration
config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint = bos_host)
#導入BosClient配置文件
#導入BOS相關模塊
from baidubce import exception
from baidubce.services import bos
from baidubce.services.bos import canned_acl
from baidubce.services.bos.bos_client import BosClient
#新建BosClient
bos_client = BosClient(config)
#uploadobject
data1 = open(filePath, 'rb')
#以數據流形式上傳Object,用戶需要自行計算數據長度content_length
#用戶需自行計算content_md5.計算方法是對數據執行md5算法獲取128位二進制數據,再進行base64編碼
bucket_name = bucket
object_key = key3
content_length = fsize
content_md5 = sfmd5(filePath)
bos_client.put_object(bucket_name, object_key, data1, content_length,content_md5)
#reportthepdf
result0 = requests.Session().post(url=Url, data=data2, verify=False, headers={"Content-Type": "application/json;charset=utf-8;"})
print('result0.status_code:', result0.status_code)
if result0.status_code == 200:
result_str = result0.text
print('result_str:', result_str)
result_json = json.loads(result_str)
code = result_json['status']['code']
msg = result_json['status']['msg']
result_json = json.loads(result_str)
print(result_json['data'])
0條評論