-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupload.py
More file actions
executable file
·114 lines (91 loc) · 4.29 KB
/
Copy pathupload.py
File metadata and controls
executable file
·114 lines (91 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'MFC'
__time__ = '2019-08-13 16:35'
# test upload method
import datetime
import random
import time
import requests
import upyun
class FileCtrl:
def __init__(self):
self.IMAGE_DOMAIN = 'https://pic.huodongjia.com/'
def download_image_from_url(self, url):
'''
访问外部图片, 返回 file obj
'''
try:
#print('prepare fetch image URL........{}'.format(url))
start_time = time.time()
image = requests.get(url, stream=True, timeout=7, verify=False)
image_type = image.headers['Content-Type'].split('/')[-1] # get image type str
if image_type in ["png", "jpeg", "jpg", "gif", "bmp", 'webp', "octet-stream"]:
image_size = int(image.headers.get('content-length', -1))/1024
if image_size < 0:
print('image.headers have no content-length, do not download')
return None, None
if image_size > 1000:
print('image size > 1M, do not download')
return None, None
print('finish fetch image URL........{}'.format(url))
print('fetch image cost time : {}'.format(time.time() - start_time))
else:
print('image_type is error, current type : {}'.format(image_type))
return None, None
except TimeoutError:
print('download outside url failed. caused by timeout')
return None, None
else:
# file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S') + f".{image_type}"
# download_path = os.getcwd()+"/tmp_img/" + file_name
# with open(download_path, 'wb') as f:
# f.write(image.content)
# return download_path, file_name # old return image.raw
return image.content, image_type # old return image.raw
def upload_file_by_http(self, file_obj, image_type, directory='event-content'):
'''
use upyun sdk to upload file -- please test before upload yx
'''
BUCKETNAME = 'upyun_info'
USERNAME = 'upyun_info'
PASSWORD = 'upyun_info'
up = upyun.UpYun(BUCKETNAME, USERNAME, PASSWORD, timeout=20, endpoint=upyun.ED_AUTO)
up.up_rest.endpoint = upyun.ED_AUTO
sub_directory = datetime.date.strftime(datetime.date.today(),
'%Y-%m-%d')
server_directory = directory + '/' + sub_directory + '/'
suffix = "." + image_type
print("Server directory is {}".format(server_directory))
upload_filename = str(time.time()) + str(random.randint(0, 9999)) + suffix
try:
up.put(server_directory + upload_filename, file_obj)
except upyun.UpYunServiceException as se:
print('Except an UpYunServiceException ...')
print('Request Id: ' + se.request_id)
print('HTTP Status Code: ' + str(se.status))
print('Error Message: ' + se.msg + '\n')
except upyun.UpYunClientException as ce:
print('Except an UpYunClientException ...')
print('Error Message: ' + ce.msg + '\n')
else:
print(self.IMAGE_DOMAIN + server_directory + upload_filename)
return self.IMAGE_DOMAIN + server_directory + upload_filename
def point_upload(self, file_path='/Users/TesterCC/ACG/web_logo/requests.png'):
# http://docs.upyun.com/api/sdk/#_7
from upyun import FileStore
from upyun import print_reporter
BUCKETNAME = ''
USERNAME = ''
PASSWORD = ''
up = upyun.UpYun(BUCKETNAME, USERNAME, PASSWORD, timeout=20, endpoint=upyun.ED_AUTO)
with open('{}'.format(file_path), 'rb') as f:
res = up.put('/up/dd.png', f, checksum=True, need_resume=True, headers={}, store=None,
reporter=print_reporter)
print(res)
print(up.getinfo('/up/dd.png'))
if __name__ == '__main__':
file_ctrl = FileCtrl()
# image_obj, image_type =file_ctrl.download_image_from_url("http://www.eshow365.com/UserUpload/ZhanHui/Title/201902280224443782.png")
# file_ctrl.upload_file_by_http(image_obj,image_type,directory='expo-content')
file_ctrl.point_upload()