Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed OverflowError problem when uploading files over 3GB to the server #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ dmypy.json
.pyre/

# main running file
main.py
main.py

# .idea
.idea
86 changes: 39 additions & 47 deletions BunnyCDNStorage.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -45,59 +43,53 @@ 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
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
if file_path is None:
file_path = file_name

response=requests.request("PUT",request_url,data=file_data,headers=self.headers)
if cdn_path[-1] == '/':
cdn_path = cdn_path[:-1]

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())
request_url = self.base_url + cdn_dir
response = requests.request('DELETE', request_url, headers=self.headers)
return response.json()