使用GoogleSignIn和GoogleAPIClientForREST,我已经能够登录谷歌,甚至可以使用Google Drive iOS quickstart中的代码获取文件列表。真的我不明白的是如何按文件类型过滤掉请求的结果,或者真正如何获取子文件夹的内容。 Google Drive REST documentation根本没有帮助。
澄清问题:而
let query = GTLRDriveQuery_FilesList.query()
query.pageSize = 10
service.executeQuery(query,
delegate: self,
didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
与
完美配合func displayResultWithTicket(ticket: GTLRServiceTicket,
finishedWithObject result : GTLRDrive_FileList,
error : NSError?) {
if let error = error {
print(error.localizedDescription)
return
}
var text = "";
if let files = result.files, !files.isEmpty {
text += "Files:\n"
for file in files {
text += "\(file.name!) (\(file.identifier!))\n"
}
} else {
text += "No files found."
}
print(text)
}
,我不知道如何只获取文件夹或如何获取子文件夹中的文件。
答案 0 :(得分:3)
对于列出文件夹,我们要声明mimeType就像这样
let root = "(mimeType = 'application/vnd.google-apps.folder' or mimeType = 'image/jpeg' or mimeType = 'image/png' or mimeType = 'application/pdf' or mimeType = 'application/epub' or mimeType = 'application/msword' or mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') and '\(root)' in parents and trashed=false"
现在,您可以传递此根目录,仅列出您所需的需求
func GoogleDrivelistFiles(root : String) { //root
let query = GTLRDriveQuery_FilesList.query()
query.pageSize = 100
query.q = root
query.fields = "files(id,name,mimeType,modifiedTime,createdTime,fileExtension,size,parents,kind),nextPageToken"
service.executeQuery(query, completionHandler: {(ticket, files, error) in
if let filesList : GTLRDrive_FileList = files as? GTLRDrive_FileList {
var cloudArray = Array<FileObject>()
if let filesShow : [GTLRDrive_File] = filesList.files {
for Array in filesShow {
let name = Array.name
let mimeType = Array.mimeType
let id = Array.identifier
let folder = (mimeType as NSString?)?.pathExtension
let isfolder = true
let parents = Array.parents
var parentPath : String!
for arrayParents in parents! {
parentPath = arrayParents
}
let creationTime = Array.createdTime?.date
let modifiedTime = Array.modifiedTime?.date
let fileSize : Int!
let fileExtension : String!
if isfolder == true {
fileSize = 0
fileExtension = "Folder"
}else {
fileSize = Array.size?.intValue
fileExtension = Array.fileExtension
}
if folder == "folder" {
let content = FileObject(path: mimeType!, isFolder: true, parentPath: parentPath!, fileName: name!, creationDate: creationTime!, lastModified: modifiedTime!, fileSize: fileSize!, fileExtension: fileExtension!, fileId: id!, isSyncing: true)
cloudArray.append(content)
} else {
let content = FileObject(path: mimeType!, isFolder: false, parentPath: parentPath!, fileName: name!, creationDate: creationTime!, lastModified: modifiedTime!, fileSize: fileSize!, fileExtension: fileExtension!, fileId: id!, isSyncing: true)
cloudArray.append(content)
}
}
}
DispatchQueue.main.async {
self.delegate?.DataRefresh(fileObject: cloudArray)
}
}else {
}
})
}
为了获得subFolder,您也可以在didSelectTable上写,并且您希望将此根传递给相同的函数GoogleDriveListFiles()。
//ForGeting SubFolders//
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let FolderId = array[indexpath.row].id
//important step
let FolderIdroot = "(mimeType = 'application/vnd.google-apps.folder' or mimeType = 'image/jpeg' or mimeType = 'image/png' or mimeType = 'application/pdf' or mimeType = 'application/epub' or mimeType = 'application/msword' or mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') and '\(FolderId)' in parents and trashed=false"
//now you can call that fun which I have already written for listingFiles func GoogleDrivelistFiles(root :String)//
GoogleDrivelistFiles(root : FolderIdRoot)
}
答案 1 :(得分:0)
嘿,您可以使用以下功能获取所有文件夹列表:-
func getAllfolders() {
let root = "mimeType = 'application/vnd.google-apps.folder'"
let query = GTLRDriveQuery_FilesList.query()
query.pageSize = 100
query.q = root
query.fields = "files(id,name,mimeType,modifiedTime,createdTime,fileExtension,size),nextPageToken"
service.executeQuery(query, completionHandler: {(ticket, files, error) in
if let filesList : GTLRDrive_FileList = files as? GTLRDrive_FileList {
if let filesShow : [GTLRDrive_File] = filesList.files {
print("files \(filesShow)")
for ArrayList in filesShow {
let name = ArrayList.name ?? ""
let id = ArrayList.identifier ?? ""
print("hello\(name)", id)
}
}
}
})
}
答案 2 :(得分:0)
您可以获取所有文件夹列表,包括/不包括已删除文件夹:-
func getAllfolders() {
let root = "mimeType = 'application/vnd.google-apps.folder'" // including trash folder or add and trashed=true
//let root = "mimeType = 'application/vnd.google-apps.folder' and trashed=false" // excluding trash folders
let query = GTLRDriveQuery_FilesList.query()
query.pageSize = 100
query.q = root
query.fields = "files(id,name,mimeType,modifiedTime,createdTime,fileExtension,size),nextPageToken"
service.executeQuery(query, completionHandler: {(ticket, files, error) in
if let filesList : GTLRDrive_FileList = files as? GTLRDrive_FileList {
if let filesShow : [GTLRDrive_File] = filesList.files {
print("files \(filesShow)")
for ArrayList in filesShow {
let name = ArrayList.name ?? ""
let id = ArrayList.identifier ?? ""
print("hello\(name)", id)
}
}
}
})
}