Dooratre commited on
Commit
1481fe5
·
verified ·
1 Parent(s): 567466c

Update upload.py

Browse files
Files changed (1) hide show
  1. upload.py +96 -9
upload.py CHANGED
@@ -53,7 +53,7 @@ def upload_image_to_cdn(base_url, auth_token, image_path):
53
  response.raise_for_status()
54
  return response.json()
55
 
56
- def upload_to_oss(self, sts_data, image_path):
57
  auth = oss2.StsAuth(
58
  sts_data['access_key_id'],
59
  sts_data['access_key_secret'],
@@ -63,21 +63,20 @@ def upload_image_to_cdn(base_url, auth_token, image_path):
63
  endpoint = f"https://{sts_data['region']}.aliyuncs.com"
64
  bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
65
 
66
- with open(image_path, 'rb') as file:
67
  result = bucket.put_object(sts_data['file_path'], file)
68
 
69
  return result.status == 200
70
 
71
- def upload_image(self, image_path):
72
- filename = os.path.basename(image_path)
73
- filesize = os.path.getsize(image_path)
74
- filetype = 'image' # Modify if you need specific MIME type detection
75
 
76
  # Step 1: Get STS token
77
  sts_data = self.get_sts_token(filename, filesize, filetype)
78
 
79
  # Step 2: Upload to OSS
80
- upload_success = self.upload_to_oss(sts_data, image_path)
81
 
82
  if upload_success:
83
  return sts_data['file_url']
@@ -87,7 +86,95 @@ def upload_image_to_cdn(base_url, auth_token, image_path):
87
  # Initialize the uploader and perform the upload
88
  uploader = ImageUploader(base_url, auth_token)
89
  try:
90
- file_url = uploader.upload_image(image_path)
91
  return file_url
92
  except Exception as e:
93
- raise Exception(f"Image upload failed: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  response.raise_for_status()
54
  return response.json()
55
 
56
+ def upload_to_oss(self, sts_data, file_path):
57
  auth = oss2.StsAuth(
58
  sts_data['access_key_id'],
59
  sts_data['access_key_secret'],
 
63
  endpoint = f"https://{sts_data['region']}.aliyuncs.com"
64
  bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
65
 
66
+ with open(file_path, 'rb') as file:
67
  result = bucket.put_object(sts_data['file_path'], file)
68
 
69
  return result.status == 200
70
 
71
+ def upload_file(self, file_path, filetype):
72
+ filename = os.path.basename(file_path)
73
+ filesize = os.path.getsize(file_path)
 
74
 
75
  # Step 1: Get STS token
76
  sts_data = self.get_sts_token(filename, filesize, filetype)
77
 
78
  # Step 2: Upload to OSS
79
+ upload_success = self.upload_to_oss(sts_data, file_path)
80
 
81
  if upload_success:
82
  return sts_data['file_url']
 
86
  # Initialize the uploader and perform the upload
87
  uploader = ImageUploader(base_url, auth_token)
88
  try:
89
+ file_url = uploader.upload_file(image_path, filetype='image')
90
  return file_url
91
  except Exception as e:
92
+ raise Exception(f"Image upload failed: {str(e)}")
93
+
94
+
95
+ def upload_audio_to_cdn(base_url, auth_token, audio_path):
96
+ """
97
+ Uploads an audio file to the CDN using STS credentials.
98
+
99
+ Args:
100
+ base_url (str): The base URL of the API endpoint (e.g., "https://chat.qwen.ai").
101
+ auth_token (str): The Bearer token for authentication.
102
+ audio_path (str): The local path to the audio file to upload.
103
+
104
+ Returns:
105
+ str: The CDN URL where the audio is accessible.
106
+
107
+ Raises:
108
+ Exception: If the upload fails at any step.
109
+ """
110
+ class AudioUploader:
111
+ def __init__(self, base_url, auth_token):
112
+ self.session = requests.Session()
113
+ self.base_url = base_url
114
+ self.auth_token = auth_token
115
+ self.headers = self._create_headers()
116
+ self.cookies = RequestsCookieJar()
117
+
118
+ def _create_headers(self):
119
+ return {
120
+ 'x-request-id': str(uuid.uuid4()),
121
+ 'Authorization': f'Bearer {self.auth_token}',
122
+ 'Content-Type': 'application/json',
123
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
124
+ }
125
+
126
+ def get_sts_token(self, filename, filesize, filetype):
127
+ endpoint = '/api/v1/files/getstsToken'
128
+ payload = {
129
+ 'filename': filename,
130
+ 'filesize': filesize,
131
+ 'filetype': filetype
132
+ }
133
+
134
+ response = self.session.post(
135
+ f'{self.base_url}{endpoint}',
136
+ json=payload,
137
+ headers=self.headers,
138
+ cookies=self.cookies
139
+ )
140
+
141
+ response.raise_for_status()
142
+ return response.json()
143
+
144
+ def upload_to_oss(self, sts_data, file_path):
145
+ auth = oss2.StsAuth(
146
+ sts_data['access_key_id'],
147
+ sts_data['access_key_secret'],
148
+ sts_data['security_token']
149
+ )
150
+
151
+ endpoint = f"https://{sts_data['region']}.aliyuncs.com"
152
+ bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
153
+
154
+ with open(file_path, 'rb') as file:
155
+ result = bucket.put_object(sts_data['file_path'], file)
156
+
157
+ return result.status == 200
158
+
159
+ def upload_file(self, file_path, filetype):
160
+ filename = os.path.basename(file_path)
161
+ filesize = os.path.getsize(file_path)
162
+
163
+ # Step 1: Get STS token
164
+ sts_data = self.get_sts_token(filename, filesize, filetype)
165
+
166
+ # Step 2: Upload to OSS
167
+ upload_success = self.upload_to_oss(sts_data, file_path)
168
+
169
+ if upload_success:
170
+ return sts_data['file_url']
171
+ else:
172
+ raise Exception("Upload failed")
173
+
174
+ # Initialize the uploader and perform the upload
175
+ uploader = AudioUploader(base_url, auth_token)
176
+ try:
177
+ file_url = uploader.upload_file(audio_path, filetype='audio')
178
+ return file_url
179
+ except Exception as e:
180
+ raise Exception(f"Audio upload failed: {str(e)}")