在某些方面,我猜你可以说我正在创建类似于iOS应用程序’Vine’的东西。一个社交网络,允许用户上传视频供他人观看。我的整个项目已经准备就绪,我…
迟到的回应,但这可能有助于其他人解决这个问题。此代码将视频上传到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!"); }
希望这可以帮助 :)
是的,云存储用于文件上传。 Parse使用的术语比Google使用的术语更简单。对于谷歌,你想阅读 这个文件 ,特别是“使用XML API实现可恢复的上传”,但阅读了整个内容。
实际上,Google服务的Objective-C包装器库在这里有非常详细的文档: https://code.google.com/p/google-api-objectivec-client/wiki/Introduction#Preparing_to_Use_the_Library
它告诉您这个包装器库是基于底层JSON API自动生成的。因此,所有GTLStorage ...类都将模仿Google Storage的JSON API。执行实际API调用的类是GTLQueryStorage。
如果查看该JSON文档,您会发现有一个用于将数据存储到存储桶中的对象: https://developers.google.com/storage/docs/json_api/v1/#Objects 上传新对象的方法是“插入”
回到GTLQueryStorage.h文件,您将找到相应的Objective-C方法将新对象插入到存储桶中:
// Method: storage.objects.insert // Stores new data blobs and associated metadata. // Required: // bucket: Name of the bucket in which to store the new object. Overrides the // provided object metadata's bucket value, if any. // Optional: // ifGenerationMatch: Makes the operation conditional on whether the object's // current generation matches the given value. // ifGenerationNotMatch: Makes the operation conditional on whether the // object's current generation does not match the given value. // ifMetagenerationMatch: Makes the operation conditional on whether the // object's current metageneration matches the given value. // ifMetagenerationNotMatch: Makes the operation conditional on whether the // object's current metageneration does not match the given value. // name: Name of the object. Required when the object metadata is not // otherwise provided. Overrides the object metadata's name value, if any. // projection: Set of properties to return. Defaults to noAcl, unless the // object resource specifies the acl property, when it defaults to full. // kGTLStorageProjectionFull: Include all properties. // kGTLStorageProjectionNoAcl: Omit the acl property. // Upload Parameters: // Accepted MIME type(s): */* // Authorization scope(s): // kGTLAuthScopeStorageDevstorageFullControl // kGTLAuthScopeStorageDevstorageReadWrite // Fetches a GTLStorageObject. + (id)queryForObjectsInsertWithObject:(GTLStorageObject *)object bucket:(NSString *)bucket uploadParameters:(GTLUploadParameters *)uploadParametersOrNil;
所以你应该: