简体   繁体   English

上传Google云端存储iOS

[英]Upload Google Cloud Storage iOS

I'm trying to upload image to Google Cloud Storage for a couple days. 我正在尝试将图片上传到Google云端存储空间几天。 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 . 我尝试通过此链接https://code.google.com/p/google-api-objectivec-client/wiki/Introduction使用适用于Objective-C的Google API客户端库。

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)" . 使用该代码时,错误始终显示为“操作无法完成。(访问未配置。请使用Google Developers Console激活项目的API)” 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. 我不知道为什么会出现错误,而我已启用与Google云端存储和Google云端存储JSON API等存储相关的所有服务,并更改了存储分区的ACL,以便互联网上的所有用户都可以写入/上传到其中。

I also tried authentication process before upload. 我还在上传前尝试过身份验证过程。 The code on viewDidLoad: 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. 非常感谢你。

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 或者,如果您要在未经身份验证的情况下上传,则必须为服务器应用程序分配API Key with Key,而不是Key for iOS应用程序

_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 我不知道为什么上传过程成功使用Key for server应用程序,而这是一个iOS应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM