better upload method
This commit is contained in:
parent
8326a75a85
commit
3b3308a8cb
|
@ -14,32 +14,42 @@ def vprint(data):
|
||||||
global verbose
|
global verbose
|
||||||
if verbose:
|
if verbose:
|
||||||
print(data)
|
print(data)
|
||||||
|
# HERE IS OUR GENERATOR
|
||||||
|
def read_in_chunks(file_object, CHUNK_SIZE=1024):
|
||||||
|
while True:
|
||||||
|
data = file_object.read(CHUNK_SIZE)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
yield data
|
||||||
def upload(csvPath):
|
def upload(csvPath):
|
||||||
global serverurl
|
global serverurl
|
||||||
global auth
|
global auth
|
||||||
vprint("Generating nonce uuid")
|
vprint("Generating nonce uuid")
|
||||||
uid=str(uuid.uuid4())
|
uid = uuid.uuid4()
|
||||||
data=auth+"\n"+uid+"\nBEGIN"
|
content_name = str(csvPath)
|
||||||
requests.post(serverurl, data=data)
|
content_path = os.path.abspath(file)
|
||||||
vprint("Sent BEGIN")
|
content_size = os.stat(content_path).st_size
|
||||||
accumulator=""
|
print(content_name, content_path, content_size)
|
||||||
index=0
|
|
||||||
for line in open(csvPath):
|
file_object = open(content_path, "rb")
|
||||||
if index==0:
|
index = 0
|
||||||
data=auth+"\n"+uid+"\nWRITE\n"
|
offset = 0
|
||||||
accumulator=data
|
headers = {}
|
||||||
accumulator+=line
|
|
||||||
index+=1
|
for chunk in read_in_chunks(file_object, CHUNK_SIZE):
|
||||||
if index>chunkRowNum:
|
offset = index + len(chunk)
|
||||||
vprint("Sent WRITE")
|
headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset - 1, content_size)
|
||||||
requests.post(serverurl, data=accumulator)
|
headers['Authorization'] = auth
|
||||||
index=0
|
headers['X-Nonce'] = uid
|
||||||
requests.post(serverurl, data=accumulator)
|
index = offset
|
||||||
vprint("Sent WRITE")
|
try:
|
||||||
data=auth+"\n"+uid+"\nCONCLUDE"
|
|
||||||
requests.post(serverurl, data=data)
|
file = {"file": chunk}
|
||||||
vprint("Sent CONCLUDE")
|
r = requests.post(serverUrl, files=file, headers=headers)
|
||||||
|
print(r.text)
|
||||||
|
print("r: %s, Content-Range: %s" % (r, headers['Content-Range']))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
if not storage.exists("lastUpdate"):
|
if not storage.exists("lastUpdate"):
|
||||||
vprint("Last database update record has been initialized")
|
vprint("Last database update record has been initialized")
|
||||||
storage.save("lastUpdate",0)
|
storage.save("lastUpdate",0)
|
||||||
|
|
Reference in New Issue
Block a user