[英]How can I create a new folder with Google Drive API in Python?
From this example .从 这个例子。 Can I use MediafileUpload with creating folder?
我可以在创建文件夹时使用 MediafileUpload 吗? How can I get the parent_id from?
我怎样才能得到 parent_id 从?
From https://developers.google.com/drive/folder来自https://developers.google.com/drive/folder
I just know that i should use mime = "application/vnd.google-apps.folder" but how do I implement this tutorial to programming in Python?我只知道我应该使用 mime = "application/vnd.google-apps.folder" 但我如何实施本教程以使用 Python 进行编程?
Thank you for your suggestions.谢谢你的建议。
To create a folder on Drive, try:要在云端硬盘上创建文件夹,请尝试:
def createRemoteFolder(self, folderName, parentID = None):
# Create a folder on Drive, returns the newely created folders ID
body = {
'title': folderName,
'mimeType': "application/vnd.google-apps.folder"
}
if parentID:
body['parents'] = [{'id': parentID}]
root_folder = drive_service.files().insert(body = body).execute()
return root_folder['id']
You only need a parent ID here if you want to create folder within another folder, otherwise just don't pass any value for that.如果你想在另一个文件夹中创建文件夹,你只需要一个父 ID,否则不要为此传递任何值。
If you want the parent ID, you'll need to write a method to search Drive for folders with that parent name in that location (do a list() call) and then get the ID of that folder.如果您想要父 ID,则需要编写一种方法来在云端硬盘中搜索该位置中具有该父名称的文件夹(执行 list() 调用),然后获取该文件夹的 ID。
Edit: Note that v3 of the API uses a list for the 'parents' field, instead of a dictionary.编辑:请注意,API 的 v3 使用列表作为“父母”字段,而不是字典。 Also, the
'title'
field changed to 'name'
, and the insert()
method changed to create()
.此外,
'title'
字段更改为'name'
, insert()
方法更改为create()
。 The code from above would change to the following for v3:对于 v3,上面的代码将更改为以下内容:
def createRemoteFolder(self, folderName, parentID = None):
# Create a folder on Drive, returns the newely created folders ID
body = {
'name': folderName,
'mimeType': "application/vnd.google-apps.folder"
}
if parentID:
body['parents'] = [parentID]
root_folder = drive_service.files().create(body = body).execute()
return root_folder['id']
The mediafile uplaod is needed only if you want to insert content.仅当您要插入内容时才需要上传媒体文件。 Since you want only to insert metadata (folders are only metadata), you don't need it.
由于您只想插入元数据(文件夹只是元数据),因此您不需要它。 A regular POST with the JSON representing the foder is enough.
一个带有 JSON 的常规 POST 表示 foder 就足够了。
You can get the parent ID in several ways:您可以通过多种方式获取父 ID:
https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ
https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ
://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ0B8VrsrGIcVbrRDVxMXFWVkdfejQ
How to get an FileID programatically:如何以编程方式获取 FileID:
root
for the root folder of your Drive.root
代表您的云端硬盘的根文件夹。 Using 3. and 1., you can get all the fileIds of your Drive.使用 3. 和 1.,您可以获得驱动器的所有 fileId。
I dont know how I can be clearer我不知道怎样才能更清楚
def create_folder(header, folder_name, folder_id, drive_id=None):
url = 'https://www.googleapis.com/upload/drive/v3/files'
file_metadata = {
'name': folder_name,
'mimeType': 'application/vnd.google-apps.folder',
'parents': [folder_id]
}
file_withmetadata = {"data": ("metadata", json.dumps(file_metadata), "application/json; charset=UTF-8")}
param = {"q":"'%s' in parents" %folder_id, "supportsAllDrives":"true"}
if drive_id is not None:
param['driveId'] = drive_id
r = requests.post(
url,
headers=header,
params=param,
files=file_withmetadata
)
return json.loads(r.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.