如何使用Google Drive SDK for iPhone在Google云端硬盘上创建文件夹?

时间:2022-11-20 07:26:23

I am using Google Drive SDK for iPhone and trying to upload Audio file in "TestAudio" folder.If "TestAudio" folder is not created at google drive then first create that folder and after that my audio should store to that folder only. Every thing is working gr8 except folder creation. can any buddy please help?

我正在使用适用于iPhone的Google Drive SDK并尝试在“TestAudio”文件夹中上传音频文件。如果未在Google云端硬盘中创建“TestAudio”文件夹,则首先创建该文件夹,之后我的音频应仅存储到该文件夹​​。除文件夹创建外,每件事都在工作。任何哥们请帮忙吗?

I am using below code for upload audio file.

我使用下面的代码上传音频文件。

GTLUploadParameters *uploadParameters = nil;

NSString *soundFilePath = [[NSBundle mainBundle]
                           pathForResource:@"honey_bunny_new"
                           ofType:@"mp3"];

if (soundFilePath) {
    NSData *fileContent = [[NSData alloc] initWithContentsOfFile:soundFilePath];
    uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"audio/mpeg"];
}

self.driveFile.title = self.updatedTitle;
GTLQueryDrive *query = nil;
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0) {
    // This is a new file, instantiate an insert query.
    query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                        uploadParameters:uploadParameters];
} else {
    // This file already exists, instantiate an update query.
    query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
                                                  fileId:self.driveFile.identifier
                                        uploadParameters:uploadParameters];
}
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"Saving file"
                                                         delegate:self];

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFile *updatedFile,
                                                          NSError *error) {
[alert dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil) {
    self.driveFile = updatedFile;
    self.originalContent = [self.textView.text copy];
    self.updatedTitle = [updatedFile.title copy];
    [self toggleSaveButton];
    [self.delegate didUpdateFileWithIndex:self.fileIndex
                                    driveFile:self.driveFile];
    [self doneEditing:nil];
} else {
    NSLog(@"An error occurred: %@", error);
    [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                           message:error.description
                                          delegate:self];
}
}];

1 个解决方案

#1


1  

I don't see your code to create a folder, but I was having the same problem with folder creation myself. As you know, the mimeType must be "application/vnd.google-apps.folder". I ran into assert failures if the NSData parameter to uploadParametersWithData was nil. So I tried a zero length NSData object and that failed. Using a 1 byte NSData object also failed. The trick is to call queryForFilesUpdateWithObject with uploadParameters:nil. Then the folder creation works fine. I also discovered that the Objective-C code shown at the end of:

我没有看到你的代码创建一个文件夹,但我自己也遇到了与文件夹创建相同的问题。如您所知,mimeType必须是“application / vnd.google-apps.folder”。如果uploadParametersWithData的NSData参数为nil,我遇到断言失败。所以我尝试了一个零长度的NSData对象,但失败了。使用1字节的NSData对象也失败了。诀窍是使用uploadParameters:nil调用queryForFilesUpdateWithObject。然后文件夹创建工作正常。我还发现最后显示的Objective-C代码:

https://developers.google.com/drive/v2/reference/files/insert

https://developers.google.com/drive/v2/reference/files/insert

is incorrect. The file.parents should be as follows:

是不正确的。 file.parents应如下所示:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentID;
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil];

#1


1  

I don't see your code to create a folder, but I was having the same problem with folder creation myself. As you know, the mimeType must be "application/vnd.google-apps.folder". I ran into assert failures if the NSData parameter to uploadParametersWithData was nil. So I tried a zero length NSData object and that failed. Using a 1 byte NSData object also failed. The trick is to call queryForFilesUpdateWithObject with uploadParameters:nil. Then the folder creation works fine. I also discovered that the Objective-C code shown at the end of:

我没有看到你的代码创建一个文件夹,但我自己也遇到了与文件夹创建相同的问题。如您所知,mimeType必须是“application / vnd.google-apps.folder”。如果uploadParametersWithData的NSData参数为nil,我遇到断言失败。所以我尝试了一个零长度的NSData对象,但失败了。使用1字节的NSData对象也失败了。诀窍是使用uploadParameters:nil调用queryForFilesUpdateWithObject。然后文件夹创建工作正常。我还发现最后显示的Objective-C代码:

https://developers.google.com/drive/v2/reference/files/insert

https://developers.google.com/drive/v2/reference/files/insert

is incorrect. The file.parents should be as follows:

是不正确的。 file.parents应如下所示:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentID;
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil];