Spaces:
Sleeping
Sleeping
Rename packages.txt to upload.py
Browse files- packages.txt +0 -3
- upload.py +93 -0
packages.txt
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
ffmpeg
|
2 |
-
libsm6
|
3 |
-
libxext6
|
|
|
|
|
|
|
|
upload.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
import requests
|
4 |
+
import oss2
|
5 |
+
from requests.cookies import RequestsCookieJar
|
6 |
+
|
7 |
+
def upload_image_to_cdn(base_url, auth_token, image_path):
|
8 |
+
"""
|
9 |
+
Uploads an image to the CDN using STS credentials.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
base_url (str): The base URL of the API endpoint (e.g., "https://chat.qwen.ai").
|
13 |
+
auth_token (str): The Bearer token for authentication.
|
14 |
+
image_path (str): The local path to the image file to upload.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
str: The CDN URL where the image is accessible.
|
18 |
+
|
19 |
+
Raises:
|
20 |
+
Exception: If the upload fails at any step.
|
21 |
+
"""
|
22 |
+
class ImageUploader:
|
23 |
+
def __init__(self, base_url, auth_token):
|
24 |
+
self.session = requests.Session()
|
25 |
+
self.base_url = base_url
|
26 |
+
self.auth_token = auth_token
|
27 |
+
self.headers = self._create_headers()
|
28 |
+
self.cookies = RequestsCookieJar()
|
29 |
+
|
30 |
+
def _create_headers(self):
|
31 |
+
return {
|
32 |
+
'x-request-id': str(uuid.uuid4()),
|
33 |
+
'Authorization': f'Bearer {self.auth_token}',
|
34 |
+
'Content-Type': 'application/json',
|
35 |
+
'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'
|
36 |
+
}
|
37 |
+
|
38 |
+
def get_sts_token(self, filename, filesize, filetype):
|
39 |
+
endpoint = '/api/v1/files/getstsToken'
|
40 |
+
payload = {
|
41 |
+
'filename': filename,
|
42 |
+
'filesize': filesize,
|
43 |
+
'filetype': filetype
|
44 |
+
}
|
45 |
+
|
46 |
+
response = self.session.post(
|
47 |
+
f'{self.base_url}{endpoint}',
|
48 |
+
json=payload,
|
49 |
+
headers=self.headers,
|
50 |
+
cookies=self.cookies
|
51 |
+
)
|
52 |
+
|
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'],
|
60 |
+
sts_data['security_token']
|
61 |
+
)
|
62 |
+
|
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']
|
84 |
+
else:
|
85 |
+
raise Exception("Upload failed")
|
86 |
+
|
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)}")
|