迟到的回应,但这可能会帮助其他人解决这个问题。此代码将视频上传到 GCS(经过测试和工作)
先认证
这将弹出 Google 身份验证视图控制器(您必须将“GTMOAuth2ViewTouch.xib”文件添加到您的项目中)。
- (void) uploadVideoToGoogleCloud {
// declare this
//@property (strong, nonatomic) GTLServiceStorage* serviceStorage;
//@property (strong, nonatomic) NSString* accessToken;
_serviceStorage = [[GTLServiceStorage alloc] init];
_serviceStorage = [GTLServiceStorage new];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": @"yourGoogleProjectId"};
// authenticate
GTMOAuth2ViewControllerTouch *oAuthVC =
[[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeStorageDevstorageReadWrite
clientID:@"yourClientId from Client ID for iOS application"
clientSecret:@"yourSecret from Client ID for iOS application"
keychainItemName:@"use nil or give a name to save in keychain"
completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
_accessToken = [NSString stringWithFormat:@"Bearer %@", [auth.parameters objectForKey:@"access_token"]];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": kProjectID, @"Content-Type": @"application/json-rpc", @"Accept": @"application/json-rpc", @"Authorization": _accessToken};
_serviceStorage.authorizer = auth;
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:oAuthVC animated:YES completion:nil];
});
}
认证后上传视频
用户认证成功后,调用此函数上传视频
// upload video file
NSString *filename = @"yourVideoFileNameNoExtension";
NSString *pathToMovie = [[NSBundle mainBundle] pathForResource:filename ofType:@".mp4"];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathToMovie];
if (fileHandle) {
GTLUploadParameters *uploadParam =
[GTLUploadParameters uploadParametersWithFileHandle:fileHandle MIMEType:@"video/mp4"];
GTLStorageObject *storageObj = [GTLStorageObject object];
storageObj.name = @"thisWillAppearOnTheBucketAsTheFilename";
GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:@"your-bucket-name" uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
NSLog(@"error:%@", error ? [error description] : @"query succeeded!");
}];
ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
unsigned long long numberOfBytesRead,
unsigned long long dataLength) {
NSLog(@"ticket: %@", ticket);
NSLog(@"read %llu from %llu bytes", numberOfBytesRead, dataLength);
};
} else {
NSLog(@"no video file handle!");
}
希望这可以帮助 :)