能够修复主要prloblem。我仍然在衰退iving a 500 error, however, with really large files. Below is the fix I implemented. def flat_file_upload(conn, fileId, chunkSize, file): ''' :param conn: AnaplanConnection object which contains authorization string, workspace ID, and model ID :param fileId: ID of the file in the Anaplan model :param chunkSize: Desired size of the chunk, in megabytes :param file: Path to the local file to be uploaded to Anaplan ''' #Setting local variables for connection details authorization = conn.authorization workspaceGuid = conn.workspaceGuid modelGuid = conn.modelGuid #Restrict users from entering a value for chunkSize greater than 50mb to prevent issues with API server if chunkSize > 50: return "Chunk size must be 50mb or less." else: #Assigning the size of the local file in MB file_size = os.stat(file).st_size / __BYTES__ file_data = "" post_header = { "Authorization": authorization, "Content-Type":"application/json" } put_header = { "Authorization": authorization, "Content-Type":"application/octet-stream" } file_metadata_start = { "id":fileId, "chunkCount":-1 } file_metadata_complete = { "id":fileId, "chunkCount": file_size / (__BYTES__ * chunkSize) } url = __base_url__ + "/" +workspaceGuid + "/models/" + modelGuid + "/files/" + fileId try: start_upload_post = requests.post(url, headers=post_header, json=file_metadata_start) start_upload_post.raise_for_status() except HTTPError as e: raise HTTPError(e) #Confirm that the metadata update for the requested file was OK before proceeding with file upload if start_upload_post.ok: complete = True #Variable to track whether file has finished uploaded with open(file, "rt") as f: #Loop through the file, reading a user-defined number of bytes until complete chunkNum = 0 while True: buf=f.readlines(__BYTES__ * chunkSize) if not buf: break file_data = "" ##Added to create new file_data for each new load for item in buf: file_data += item try: authorization_verify = anaplan_auth.verify_auth_flat_file_upload(authorization) print(authorization_verify) authorization = anaplan_auth.refresh_token(authorization) #adding re-authorization for larger files authorization_verify = anaplan_auth.verify_auth_flat_file_upload(authorization) print(authorization_verify) put_header = { "Authorization": authorization, "Content-Type":"application/octet-stream" } file_upload = requests.put(url + "/chunks/" + str(chunkNum), headers=put_header, data=file_data) print(file_upload.raise_for_status()) file_upload.raise_for_status() print("Uploading chunk " + str(chunkNum + 1) +", Status: " + str(file_upload.status_code)) except HTTPError as e: raise HTTPError(e) print("Uploading chunk " + str(chunkNum + 1) +", Status: " + str(file_upload.status_code)) if not file_upload.ok: complete = False #If the upload fails, break the loop and prevent subsequent requests. Print error to screen print("Error " + str(file_upload.status_code) + '\n' + file_upload.text) break else: chunkNum += 1 if complete: complete_upload = requests.post(url + "/complete", headers=post_header, json=file_metadata_complete) if complete_upload.ok: return "File upload complete, " + str(chunkNum) + " chunk(s) uploaded to the server." else: return "There was an error with your request: " + str(complete_upload.status_code) + " " + complete_upload.text else: return "There was an error with your request: " + str(start_upload_post.status_code) + " " + start_upload_post.text
... 查看更多