-
Notifications
You must be signed in to change notification settings - Fork 6
/
BunnyCDNStorage.py
103 lines (73 loc) · 3.25 KB
/
BunnyCDNStorage.py
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
import requests
class CDNConnector():
# constructor
def __init__(self,api_key,storage_zone,storage_zone_region='de'):
"""
creates an object for using bunnyCDN \n
api_key=Your Bunny Storage ApiKey/FTP key \n
storage_zone=Name of your storage zone \n
"""
self.headers={
'AccessKey': api_key
}
if(storage_zone_region=='de' or storage_zone_region==''):
self.base_url='https://storage.bunnycdn.com/'+storage_zone+'/'
else:
self.base_url='https://'+storage_zone_region+'.storage.bunnycdn.com/'+storage_zone+'/'
def get_storaged_objects(self,cdn_path):
"""
returns files and folders stored information stored in CDN (json data)\n
path=folder path in cdn\n
"""
request_url=self.base_url+cdn_path
if(cdn_path[-1]!='/'):
request_url=request_url+'/'
response=requests.request('GET',request_url,headers=self.headers)
return(response.json())
def get_file(self,cdn_path,download_path=None):
"""
download file from your cdn storage \n
cdn_path storage path for the file, (including file name), in cdn, use / as seperator eg, 'images/logo.png' \n
download_path (default=None, stores in your present working directory) \n
pass your desired download path with file name, will rewrite already existing files, if do not exists create them.
Note, directory will not be created
"""
if(cdn_path[-1]=='/'):
cdn_path=cdn_path[:-1]
filename=cdn_path.split('/')[-1]
request_url=self.base_url+cdn_path
response = requests.request("GET", request_url, headers=self.headers)
if(response.status_code==404):
raise ValueError('No such file exists')
if(response.status_code!=200):
raise Exception('Some error, please check all settings once and retry')
if(download_path==None):
download_path=filename
with open(download_path,'wb') as file:
file.write(response.content)
def upload_file(self,cdn_path,file_name,file_path=None):
"""
uploads your files to cdn server \n
cdn_path - directory to save in CDN \n
filename - name to save with cdn \n
file_path - locally stored file path,
if none it will look for file in present working directory
"""
if(file_path==None):
file_path=file_name
with open(file_path,'rb') as file:
file_data=file.read()
if(cdn_path[-1]=='/'):
cdn_path=cdn_path[:-1]
request_url=self.base_url+cdn_path+'/'+file_name
response=requests.request("PUT",request_url,data=file_data,headers=self.headers)
return(response.json())
def remove(self,cdn_dir):
"""
deletes a directory or file from cdn \n
cdn_dir=complete path including file on CDN \n
for directory make sure that path ends with /
"""
request_url=self.base_url+cdn_dir
response=requests.request('DELETE',request_url,headers=self.headers)
return(response.json())