首页 > 解决方案 > Explaination on error viewing status of Google Drive API v3 upload using next_chunk() in Python?

问题描述

I am trying to do a resumable upload to google drive using the v3 api. I want to be able to display a status bar of the upload. I can get it to upload easily and quickly if I do not need a status bar because I can use the .execute() function and it uploads. The problems arise when I want to upload the files in chunks. I've seen a few solutions to this on here and other places, but they don't seem to work.

This is my code for uploading:

    CHUNK_SIZE = 256*1024
    file_metadata = {'name': file_name, 'parents': [folder_id]} #Metadata for the file we are going to upload
    media = MediaFileUpload(file_path, mimetype='application/zip',chunksize=CHUNK_SIZE, resumable=True) 
    file = service.files().create(body=file_metadata, media_body=media, fields='id')
    progress = progressBarUpload(file) #create instance off progress bar class
    progress.exec_() #execute it
    progress.hide() #hide it off the screen after
    print(file_name + " uploaded successfully")
    return 1 #returns 1 if it was successful

The progress bar calls a thread for my gui which then uses the next_chunk() function, this code is here:

signal = pyqtSignal(int)
def __init__(self, file):
    super(ThreadUpload,self).__init__()
    self.file = file

def run(self):
    done = False
    while done == False:
        status, done = self.file.next_chunk()
        print("status->",status)
        if status:
            value = int(status.progress() * 100)
            print("Uploaded",value,"%")
            self.signal.emit(value)

The problem I am getting is that my status = None.

If I use this code it works correctly, but I cannot view the status of the upload using this method. There is a .execute() added which makes it work. I get rid of the next_chunk() part when doing it this way:

    CHUNK_SIZE = 256*1024
    file_metadata = {'name': file_name, 'parents': [folder_id]} #Metadata for the file we are going to upload
    media = MediaFileUpload(file_path, mimetype='application/zip',chunksize=CHUNK_SIZE, resumable=True) 
    file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()

The first method doesn't work whether I use it in the progress bar thread or not, the second method works both ways every time. I use the progress bar to view the status for downloads and a few other things and it works very well, so I'm pretty confident its the fact my status = None when downloading that is the problem.

Thanks for the help in advance.

标签: pythonpython-3.xuploadgoogle-drive-api

解决方案


问题是您正在将请求的响应(done在您的情况下为变量)与 进行比较False,此条件将永远不会返回True,因为None一旦上传过程完成,响应就是具有对象 ID 的对象或对象。这是我测试并成功运行的代码:

   CHUNK_SIZE = 256 * 1024
    file_metadata = {'name': "Test resumable"}  # Metadata for the file we are going to upload
    media = MediaFileUpload("test-image.jpg", mimetype='image/jpeg', chunksize=CHUNK_SIZE, resumable=True)
    request = service.files().create(body=file_metadata, media_body=media, fields='id')
    response = None
    while response is None:
        status, response = request.next_chunk()
        if status:
            print(status.progress())

    print(response)
    print("uploaded successfully")

对于您的情况,您可以更改以下两行:

done = False
while done == False:

为了这:

done = None
while done is None:

推荐阅读