如何使用Google云端硬盘API一次删除多个文件 [英] How to delete multiple files at once using Google Drive API

查看:372
本文介绍了如何使用Google云端硬盘API一次删除多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个python脚本,它将文件上传到驱动器中的特定文件夹,正如我注意到的那样,驱动器api为此提供了出色的实现,但是我确实遇到了一个问题,如何做我一次删除多个文件吗?
我尝试从驱动器中获取想要的文件,并整理它们的ID,但那里没有运气...(下面的代码段)

I'm developing a python script that will upload files to a specific folder in my drive, as I come to notice, the drive api provides an excellent implementation for that, but I did encountered one problem, how do I delete multiple files at once?
I tried grabbing the files I want from the drive and organize their Id's but no luck there... (a snippet below)

dir_id = "my folder Id"
file_id = "avoid deleting this file"

dFiles = []
query = ""

#will return a list of all the files in the folder
children = service.files().list(q="'"+dir_id+"' in parents").execute()

for i in children["items"]:
    print "appending "+i["title"]

    if i["id"] != file_id: 
        #two format options I tried..

        dFiles.append(i["id"]) # will show as array of id's ["id1","id2"...]  
        query +=i["id"]+", " #will show in this format "id1, id2,..."

query = query[:-2] #to remove the finished ',' in the string

#tried both the query and str(dFiles) as arg but no luck...
service.files().delete(fileId=query).execute() 

是否可以删除选定的文件(毕竟,这是一项基本操作,所以我不明白为什么不能这样做)?

Is it possible to delete selected files (I don't see why it wouldn't be possible, after all, it's a basic operation)?

提前谢谢!

推荐答案

您可以批量添加多个Drive API一起请求.这样的东西应该可以使用 Python API客户端库:

You can batch multiple Drive API requests together. Something like this should work using the Python API Client Library:

def delete_file(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
    pass
  else:
    # Do something with the response
    pass

batch = service.new_batch_http_request(callback=delete_file)

for file in children["items"]:
  batch.add(service.files().delete(fileId=file["id"]))

batch.execute(http=http)

这篇关于如何使用Google云端硬盘API一次删除多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆