Spaces:
Sleeping
Sleeping
Update upload.py
Browse files
upload.py
CHANGED
@@ -63,19 +63,8 @@ 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 |
-
# Default MIME type for images
|
67 |
-
content_type = 'image/jpeg'
|
68 |
-
if file_path.endswith('.png'):
|
69 |
-
content_type = 'image/png'
|
70 |
-
elif file_path.endswith('.gif'):
|
71 |
-
content_type = 'image/gif'
|
72 |
-
|
73 |
with open(file_path, 'rb') as file:
|
74 |
-
result = bucket.put_object(
|
75 |
-
sts_data['file_path'],
|
76 |
-
file,
|
77 |
-
headers={'Content-Type': content_type} # Set MIME type explicitly
|
78 |
-
)
|
79 |
|
80 |
return result.status == 200
|
81 |
|
@@ -97,7 +86,7 @@ def upload_image_to_cdn(base_url, auth_token, image_path):
|
|
97 |
# Initialize the uploader and perform the upload
|
98 |
uploader = ImageUploader(base_url, auth_token)
|
99 |
try:
|
100 |
-
file_url = uploader.upload_file(image_path, filetype='image
|
101 |
return file_url
|
102 |
except Exception as e:
|
103 |
raise Exception(f"Image upload failed: {str(e)}")
|
@@ -162,20 +151,8 @@ def upload_audio_to_cdn(base_url, auth_token, audio_path):
|
|
162 |
endpoint = f"https://{sts_data['region']}.aliyuncs.com"
|
163 |
bucket = oss2.Bucket(auth, endpoint, sts_data['bucketname'])
|
164 |
|
165 |
-
# Detect MIME type based on file extension
|
166 |
-
content_type = 'audio/mpeg' # Default for MP3
|
167 |
-
if file_path.endswith('.wav'):
|
168 |
-
content_type = 'audio/wav'
|
169 |
-
elif file_path.endswith('.ogg'):
|
170 |
-
content_type = 'audio/ogg'
|
171 |
-
|
172 |
-
# Upload with explicit Content-Type header
|
173 |
with open(file_path, 'rb') as file:
|
174 |
-
result = bucket.put_object(
|
175 |
-
sts_data['file_path'],
|
176 |
-
file,
|
177 |
-
headers={'Content-Type': content_type} # Set MIME type explicitly
|
178 |
-
)
|
179 |
|
180 |
return result.status == 200
|
181 |
|
@@ -183,17 +160,7 @@ def upload_audio_to_cdn(base_url, auth_token, audio_path):
|
|
183 |
filename = os.path.basename(file_path)
|
184 |
filesize = os.path.getsize(file_path)
|
185 |
|
186 |
-
#
|
187 |
-
if filename.endswith('.mp3'):
|
188 |
-
filetype = 'audio/mpeg'
|
189 |
-
elif filename.endswith('.wav'):
|
190 |
-
filetype = 'audio/wav'
|
191 |
-
elif filename.endswith('.ogg'):
|
192 |
-
filetype = 'audio/ogg'
|
193 |
-
else:
|
194 |
-
filetype = 'audio/mpeg' # Default to MP3
|
195 |
-
|
196 |
-
# Step 1: Get STS token with correct MIME type
|
197 |
sts_data = self.get_sts_token(filename, filesize, filetype)
|
198 |
|
199 |
# Step 2: Upload to OSS
|
@@ -207,7 +174,7 @@ def upload_audio_to_cdn(base_url, auth_token, audio_path):
|
|
207 |
# Initialize the uploader and perform the upload
|
208 |
uploader = AudioUploader(base_url, auth_token)
|
209 |
try:
|
210 |
-
file_url = uploader.upload_file(audio_path, filetype='audio
|
211 |
return file_url
|
212 |
except Exception as e:
|
213 |
raise Exception(f"Audio upload failed: {str(e)}")
|
|
|
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 |
|
|
|
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)}")
|
|
|
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 |
|
|
|
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
|
|
|
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)}")
|