jiayong commited on
Commit
07ec1d3
1 Parent(s): 7efa9d6

Upload oss_utils.py

Browse files
Files changed (1) hide show
  1. oss_utils.py +80 -0
oss_utils.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ import sys
4
+ import string
5
+ import time
6
+ import datetime
7
+ import json
8
+ from io import BytesIO
9
+ import oss2
10
+ import random
11
+ import requests
12
+ import shutil
13
+ from myconfigs import *
14
+
15
+ use_internal_network = False
16
+
17
+ def get_random_string():
18
+ now = datetime.datetime.now()
19
+ date = now.strftime('%Y%m%d')
20
+ time = now.strftime('%H%M%S')
21
+ microsecond = now.strftime('%f')
22
+ microsecond = microsecond[:6]
23
+
24
+ rand_num = ''.join([str(random.randint(0, 9)) for _ in range(6)])
25
+ random_string = ''.join(random.choices(string.ascii_uppercase, k=6)) # ascii_lowercase
26
+ return date + "-" + time + "-" + microsecond + "-" + random_string
27
+
28
+ class ossService():
29
+ def __init__(self):
30
+ self.AccessKeyId = OSSAccessKeyId
31
+ self.AccessKeySecret = OSSAccessKeySecret
32
+ self.Endpoint = "https://oss-cn-shanghai.aliyuncs.com"
33
+ if use_internal_network:
34
+ self.Endpoint = "https://oss-cn-shanghai-internal.aliyuncs.com"
35
+ self.BucketName = "vigen-invi" # "vigen-video"
36
+ self.ObjectName = "video_generation" # "VideoGeneration"
37
+ self.Prefix = "oss://" + self.BucketName
38
+
39
+ auth = oss2.Auth(self.AccessKeyId, self.AccessKeySecret)
40
+ self.bucket = oss2.Bucket(auth, self.Endpoint, self.BucketName)
41
+
42
+ # oss_url: eg: oss://BucketName/ObjectName/xxx.mp4
43
+ # timeout: eg: 3600*100
44
+ # params: eg: {'x-oss-process': style}
45
+ def sign(self, oss_url, timeout=3600, params=None):
46
+ try:
47
+ oss_path = oss_url[len("oss://" + self.BucketName + "/"):]
48
+ return 1, self.bucket.sign_url('GET', oss_path, timeout, slash_safe=True, params=params)
49
+ except Exception as e:
50
+ print("sign error: {}".format(e))
51
+ return 0, ""
52
+
53
+ def uploadOssFile(self, oss_full_path, local_full_path):
54
+ try:
55
+ self.bucket.put_object_from_file(oss_full_path, local_full_path)
56
+ return self.sign(self.Prefix+"/"+oss_full_path, timeout=3600)
57
+ except oss2.exceptions.OssError as e:
58
+ print("oss upload error: ", e)
59
+ return 0, ""
60
+
61
+ def downloadOssFile(self, oss_full_path, local_full_path):
62
+ status = 1
63
+ try:
64
+ self.bucket.get_object_to_file(oss_full_path, local_full_path)
65
+ except oss2.exceptions.OssError as e:
66
+ print("oss download error: ", e)
67
+ status = 0
68
+ return status
69
+
70
+
71
+ def downloadFile(self, file_full_url, local_full_path):
72
+ status = 1
73
+ response = requests.get(file_full_url)
74
+ if response.status_code == 200:
75
+ with open(local_full_path, "wb") as f:
76
+ f.write(response.content)
77
+ else:
78
+ print("oss download error. ")
79
+ status = 0
80
+ return status