mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
25 lines
1013 B
Python
25 lines
1013 B
Python
"""Utiles file of guardian"""
|
|
"""Django import"""
|
|
import oss2
|
|
"""Import setting"""
|
|
from django.conf import settings
|
|
"""Import tempfile"""
|
|
import tempfile
|
|
|
|
def upload_image_to_alibaba(image, filename):
|
|
"""upload image on oss alibaba bucket"""
|
|
# Save the image object to a temporary file
|
|
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
|
"""write image in temporary file"""
|
|
temp_file.write(image.read())
|
|
"""auth of bucket"""
|
|
auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET)
|
|
"""fetch bucket details"""
|
|
bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME)
|
|
# Upload the temporary file to Alibaba OSS
|
|
bucket.put_object_from_file(filename, temp_file.name)
|
|
"""create perfect url for image"""
|
|
new_filename = filename.replace(' ', '%20')
|
|
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
|
|