fangxia commited on
Commit
4a7a73b
1 Parent(s): 16e3a48
Files changed (1) hide show
  1. examples/oss_utils.py +0 -89
examples/oss_utils.py DELETED
@@ -1,89 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- import os
3
- import sys
4
- import string
5
- # import platform
6
- import time
7
- import datetime
8
- import json
9
- # import numpy as np
10
- # import threading
11
- # import cv2
12
- # import PIL.Image as Image
13
- # import ffmpeg
14
- from io import BytesIO
15
- # from queue import Queue
16
- # import glob
17
- import oss2
18
- import random
19
- import requests
20
- import shutil
21
- # import torch
22
- # import ctypes
23
-
24
- use_internal_network = False
25
-
26
- # OSSAccessKeyId = os.getenv('OSSAccessKeyId', "")
27
- # OSSAccessKeySecret = os.getenv('OSSAccessKeySecret', "")
28
-
29
- def get_random_string():
30
- now = datetime.datetime.now()
31
- date = now.strftime('%Y%m%d')
32
- time = now.strftime('%H%M%S')
33
- microsecond = now.strftime('%f')
34
- microsecond = microsecond[:6] # 取前6位,即微秒
35
-
36
- rand_num = ''.join([str(random.randint(0, 9)) for _ in range(6)])
37
- random_string = ''.join(random.choices(string.ascii_uppercase, k=6)) # ascii_lowercase
38
- return date + "-" + time + "-" + microsecond + "-" + random_string
39
-
40
- class ossService():
41
- def __init__(self, OSSAccessKeyId, OSSAccessKeySecret, Endpoint, BucketName, ObjectName):
42
- self.AccessKeyId = OSSAccessKeyId
43
- self.AccessKeySecret = OSSAccessKeySecret
44
- self.Endpoint = Endpoint
45
- self.BucketName = BucketName # "vigen-video"
46
- self.ObjectName = ObjectName # "VideoGeneration"
47
- self.Prefix = "oss://" + self.BucketName
48
-
49
- auth = oss2.Auth(self.AccessKeyId, self.AccessKeySecret)
50
- self.bucket = oss2.Bucket(auth, self.Endpoint, self.BucketName)
51
-
52
-
53
- # oss_url: eg: oss://BucketName/ObjectName/xxx.mp4
54
- def sign(self, oss_url, timeout=86400):
55
- try:
56
- oss_path = oss_url[len("oss://" + self.BucketName + "/"):]
57
- return 1, self.bucket.sign_url('GET', oss_path, timeout, slash_safe=True)
58
- except Exception as e:
59
- print("sign error: {}".format(e))
60
- return 0, ""
61
-
62
- def uploadOssFile(self, oss_full_path, local_full_path):
63
- try:
64
- self.bucket.put_object_from_file(oss_full_path, local_full_path)
65
- return self.sign(self.Prefix+"/"+oss_full_path, timeout=86400)
66
- except oss2.exceptions.OssError as e:
67
- print("oss upload error: ", e)
68
- return 0, ""
69
-
70
- def downloadOssFile(self, oss_full_path, local_full_path):
71
- status = 1
72
- try:
73
- self.bucket.get_object_to_file(oss_full_path, local_full_path)
74
- except oss2.exceptions.OssError as e:
75
- print("oss download error: ", e)
76
- status = 0
77
- return status
78
-
79
-
80
- def downloadFile(self, file_full_url, local_full_path):
81
- status = 1
82
- response = requests.get(file_full_url)
83
- if response.status_code == 200:
84
- with open(local_full_path, "wb") as f:
85
- f.write(response.content)
86
- else:
87
- print("oss download error. ")
88
- status = 0
89
- return status