tantul
tantul

Reputation: 81

Upload Google Cloud Storage iOS

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-api-objectivec-client/wiki/Introduction.

Here is my code implementation:

_serviceStorage = [GTLServiceStorage new];
_serviceStorage.APIKey = API_KEY;
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID};

GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];

GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query delegate:self didFinishSelector:@selector(serviceTicket:finishedWithObject:error:)];

ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
                               unsigned long long numberOfBytesRead,
                               unsigned long long dataLength) {
    self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};

With that code, the error always appear "The operation couldn't be completed. (Access Not Configured. Please use Google Developers Console to activate the API for your project)". I don't know why error shows up whereas I've enabled all services related to storage like Google Cloud Storage and Google Cloud Storage JSON API and change ACL of the bucket so that all user on the internet can write/upload into it.

I also tried authentication process before upload. The code on viewDidLoad:

_serviceStorage = [GTLServiceStorage new];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID};
GTMOAuth2ViewControllerTouch *oAuthVC = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kScope
                                                                                   clientID:kClientID
                                                                               clientSecret:kClientSecret
                                                                           keychainItemName:kKeychainItemName
                                                                          completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
                                                                              dispatch_async(dispatch_get_main_queue(), ^{
                                                                                  [self dismissViewControllerAnimated:YES completion:nil];
                                                                              });
                                                                              auth.authorizationTokenKey = @"access_token";
                                                                              _serviceStorage.authorizer = auth;
                                                                          }];

dispatch_async(dispatch_get_main_queue(), ^{
    [self presentViewController:oAuthVC animated:YES completion:nil];
});

When user press upload button:

GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];

GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query delegate:self didFinishSelector:@selector(serviceTicket:finishedWithObject:error:)];

ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
                               unsigned long long numberOfBytesRead,
                               unsigned long long dataLength) {
    self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};

With that implementation I got different error, "The operation couldn't be completed. (Insufficient Permission)".

What should I do so that I can upload image successfully? Any help will be appreciated.

Thank you very much.

Upvotes: 3

Views: 2034

Answers (1)

tantul
tantul

Reputation: 81

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

Upvotes: 5

Related Questions