From deb773ee4921f41f77ad3aefb1761e0b29770fa0 Mon Sep 17 00:00:00 2001 From: programishka Date: Wed, 2 Dec 2020 00:49:57 +0300 Subject: [PATCH 1/2] Fixed OverflowError problem when uploading files over 3GB to the server --- BunnyCDNStorage.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/BunnyCDNStorage.py b/BunnyCDNStorage.py index 415578c..0828a3d 100644 --- a/BunnyCDNStorage.py +++ b/BunnyCDNStorage.py @@ -77,18 +77,16 @@ def upload_file(self,cdn_path,file_name,file_path=None): """ 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) + with open(file_path, 'rb') as file: + response=requests.request("PUT",request_url,data=file,headers=self.headers) - return(response.json()) + return(response.json()) From 94f0237208b32f2d5e5d89801419b8138d99c523 Mon Sep 17 00:00:00 2001 From: programishka Date: Wed, 2 Dec 2020 01:08:18 +0300 Subject: [PATCH 2/2] Reformat to PEP8 --- .gitignore | 5 ++- BunnyCDNStorage.py | 84 +++++++++++++++++++++------------------------- 2 files changed, 43 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index add2d76..55ccf5b 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,7 @@ dmypy.json .pyre/ # main running file -main.py \ No newline at end of file +main.py + +# .idea +.idea \ No newline at end of file diff --git a/BunnyCDNStorage.py b/BunnyCDNStorage.py index 0828a3d..3090100 100644 --- a/BunnyCDNStorage.py +++ b/BunnyCDNStorage.py @@ -1,42 +1,40 @@ import requests -class CDNConnector(): + +class CDNConnector: # constructor - def __init__(self,api_key,storage_zone,storage_zone_region='de'): + 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 + + self.headers = { + 'AccessKey': api_key } - if(storage_zone_region=='de' or storage_zone_region==''): - self.base_url='https://storage.bunnycdn.com/'+storage_zone+'/' + 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+'/' - + self.base_url = 'https://' + storage_zone_region + '.storage.bunnycdn.com/' + storage_zone + '/' - def get_storaged_objects(self,cdn_path): + 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+'/' + request_url = self.base_url + cdn_path - response=requests.request('GET',request_url,headers=self.headers) - return(response.json()) + 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): + 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 @@ -45,29 +43,27 @@ def get_file(self,cdn_path,download_path=None): Note, directory will not be created """ - if(cdn_path[-1]=='/'): - cdn_path=cdn_path[:-1] + if cdn_path[-1] == '/': + cdn_path = cdn_path[:-1] - filename=cdn_path.split('/')[-1] + filename = cdn_path.split('/')[-1] - request_url=self.base_url+cdn_path + request_url = self.base_url + cdn_path response = requests.request("GET", request_url, headers=self.headers) - - if(response.status_code==404): + + if response.status_code == 404: raise ValueError('No such file exists') - if(response.status_code!=200): + if response.status_code != 200: raise Exception('Some error, please check all settings once and retry') - if(download_path==None): - download_path=filename + if download_path is None: + download_path = filename - with open(download_path,'wb') as file: + with open(download_path, 'wb') as file: file.write(response.content) - - - def upload_file(self,cdn_path,file_name,file_path=None): + 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 @@ -75,27 +71,25 @@ def upload_file(self,cdn_path,file_name,file_path=None): 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 - - if(cdn_path[-1]=='/'): - cdn_path=cdn_path[:-1] + if file_path is None: + file_path = file_name - request_url=self.base_url+cdn_path+'/'+file_name + if cdn_path[-1] == '/': + cdn_path = cdn_path[:-1] - with open(file_path, 'rb') as file: - response=requests.request("PUT",request_url,data=file,headers=self.headers) - - return(response.json()) + request_url = self.base_url + cdn_path + '/' + file_name + with open(file_path, 'rb') as file: + response = requests.request("PUT", request_url, data=file, headers=self.headers) + return response.json() - def remove(self,cdn_dir): + 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()) \ No newline at end of file + request_url = self.base_url + cdn_dir + response = requests.request('DELETE', request_url, headers=self.headers) + return response.json()