Edgar Navasardyan
Edgar Navasardyan

Reputation: 4501

Google Drive Api batching - getting the result of mulitple file uploads

This question is formulated on the basis of python, but the core issue concerns Drive API in general.

I have the following python code for uploading multiple files deploying batching:

batch = service.new_batch_http_request(callback=f_delete_file_batch_callback)
for item in files_list:
    filename = item.name
    file_metadata = {'title' : filename}
    fo = open(filename, "w+b")
    fo.write(item.file.read())
    media = MediaFileUpload(...)
    batch.add(service.files().insert(body=file_metadata, media_body=media))                              

http =  credential.authorize(httplib2.Http())
batch.execute(http = http)
uploaded_files = ... # Here's the question. How do I get new files?
return render(request, 'my_template.html', {'files': uploaded_files}

The problem is that I don't understand how I could fetch the newly uploaded files so that I could pass the new lines to ajax and render them on client-side:

my_template.html:

{% for item in files.items %}
    <tr>
        <td>{{item.title}}</td>
        <td>{{item.modifiedDate}}</td>
    </tr>
{% endfor %}

javascript:

$.post(url, data, function(){
    $table.append(response)
});

Upvotes: 0

Views: 618

Answers (1)

pinoyyid
pinoyyid

Reputation: 22286

From memory, the response from the batch request is an array of file resources for the newly created files. Actually it's a multipart mimetype with a part for each component request.

Poking around the python library (ugh I hate libraries), it looks like there is an optional callback to the constructor that is called for each body part. In your case you've defined this as callback=f_delete_file_batch_callback

Soooo, I would expect this callback to be called with a file object for each inserted file.

By default, the file object will only contain title, id and mimetype. If you want the modifiedDate, you'll need to add that (or *) to the fields parameter.

Upvotes: 1

Related Questions