遅い応答ですが、これはこの問題で他の人を助けるかもしれません. このコードは動画を 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!");
}
お役に立てれば :)