当前位置: 首页 > 知识库问答 >
问题:

上传谷歌云存储iOS

公西浩
2023-03-14
_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;
};
_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];
});
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;
};

我应该做什么才能成功上传图片?任何帮助都将不胜感激。

非常感谢。

共有1个答案

陶星辰
2023-03-14

最后我找到了答案。下面是身份验证的代码:

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];
                                                                                  });
                                                                              }];

下面是上传过程:

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;
};

或者,如果您想在没有身份验证的情况下上传,您必须为服务器应用程序分配API密钥,而不是为iOS应用程序分配密钥

_serviceStorage.APIKey = KEY_SERVER_APPLICATION;
 类似资料:
  • 我们一直在使用服务,在(AWS)中,我们意外删除了一个目录,因此我们认为它应该在中,但是经过查看,尽管处于打开状态,但它并不存在。

  • 追踪下面。 相关的Python片段: 最终触发(从ssl库): 溢出错误:字符串长度超过2147483647字节 我想我缺少一些特殊的配置选项? 这可能与这名1.5岁的年轻人有关,显然他还没有解决问题:https://github.com/googledatalab/datalab/issues/784. 谢谢你的帮助! 完整跟踪: [File”/usr/src/app/gcloud/downlo

  • 我想按照官方文档中提供的示例将一个文件上传到Google云存储 然而,我得到了一个错误: 线程“main”com.google.cloud.storage.StorageException中的异常:获取服务帐户的访问令牌时出错:400个错误请求{“错误”:“无效的授予”、“错误描述”:“无效的JWT:令牌必须是短期令牌(60分钟)并且在合理的时间范围内。请检查JWT声明中的iat和exp值。”位于

  • 在谷歌云存储中,我在名为图像的根桶中有一个名为猫的桶。我正在使用google-api-ruby-Client gem上传文件。我可以将文件上传到根桶“图像”,但上传到“图像/猫”不起作用。我知道谷歌云存储中的存储桶没有斜杠的概念,所以我无法弄清楚如何指定嵌套存储桶的名称。 这给出了nil:NilClass的错误

  • 我很难让GCS JSON API中的“简单上传”方法在Python中工作。文件(https://developers.google.com/storage/docs/json_api/v1/how-tos/upload#simple)使它看起来微不足道,但我无法通过授权。我不完全理解授权/身份验证/密钥/令牌,但我正在尝试。我一直试图上传的bucket允许完全读写(它们是公共的),我已经生成并尝试

  • 我正在考虑是否可以在Windows平台上使用gsutil的命令将文件上传到Google云存储。我在本地电脑上有6个文件夹,每天都有新的pdf文档添加到其中。每个文件夹包含大约2500个文件。所有文件目前都在谷歌存储在各自的文件夹中。现在我主要使用谷歌云存储管理器上传所有的新文件。有没有办法创建一个批处理文件,并安排每天晚上自动运行它,这样它就只抓取今天扫描过的文件,并将其上传到谷歌存储?