1👍
✅
Open a file and make repeatedly PUT requests to the server in chunks.
To complete the upload make a POST request with the file’s checksum.
Example
import hashlib
import os
import requests
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth(username='username', password='password')
file = 'prova.txt'
size = os.path.getsize(file)
hash_md5 = hashlib.md5()
CHUNK_SIZE = 100
with open(file, 'rb') as f:
url = 'http://localhost:8000/'
offset = 0
for chunk in iter(lambda: f.read(CHUNK_SIZE), b''):
hash_md5.update(chunk)
res = requests.put(
url,
data={'filename': 'my_new_file'},
files={'file': chunk},
headers={
'Content-Range': f'bytes {offset}-{offset + len(chunk) -1}/{size}'
},
auth=auth
)
offset = int(res.json().get('offset'))
url = res.json().get('url')
finalize = requests.post(url, data={'md5': hash_md5.hexdigest()}, auth=auth)
print(finalize.status_code)
print(finalize.json())
Source:stackexchange.com