Upload Google Cloud Storage iOS

后端 未结 1 611
梦如初夏
梦如初夏 2021-02-06 16:59

I\'m trying to upload image to Google Cloud Storage for a couple days. I tried to use Google APIs Client Library for Objective-C from this link https://code.google.com/p/google-

1条回答
  •  清歌不尽
    2021-02-06 17:52

    Finally I found the answer. Here is the code for authentication:

    GTMOAuth2ViewControllerTouch *oAuthVC = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeStorageDevstorageReadWrite
                                                                                           clientID:kClientID
                                                                                       clientSecret:kClientSecret
                                                                                   keychainItemName:kKeychainItemName
                                                                                  completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
    
                                                                                      _accessToken = [NSString stringWithFormat:@"Bearer %@", [auth.parameters objectForKey:@"access_token"]];
    
                                                                                      _serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID, @"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];
                                                                                      });
                                                                                  }];
    

    and here is for uploading process:

    GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
    GTLStorageObject *storageObj = [GTLStorageObject object];
    storageObj.name = FILE_NAME;
    
    GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
    GTLServiceTicket *ticket = [_serviceStorage executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    }];
    
    ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
                                   unsigned long long numberOfBytesRead,
                                   unsigned long long dataLength) {
        self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
    };
    

    Or if you want to upload without authentication, you have to assign API Key with Key for server applications not Key for iOS applications

    _serviceStorage.APIKey = KEY_SERVER_APPLICATION;
    

    I don't know why the uploading process successful when using Key for server applications whereas this is an iOS Application

    0 讨论(0)
提交回复
热议问题