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

查看:49
本文介绍了如何使用 Google Drive 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 Drive API 一次删除多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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