【问题标题】:Error 403 "Access not configured" writing to bucket using JSON API使用 JSON API 写入存储桶时出现错误 403“未配置访问”
【发布时间】:2013-02-06 21:08:08
【问题描述】:

我正在尝试使用以下代码通过 REST API 写入云存储:

  public static void insertData() {
                try {
                    StorageObject st = new StorageObject();
    //create the media object
                    Media m = new Media();
   String content = "hi! this is a test";
                    m.setData(Base64.encodeBase64String(content.getBytes()));
                    m.setContentType("text/html");
                    st.setMedia(m);
    //this gets me the credential, works for other APIs but not cloud storage
                    Storage storage = RequestBuilder.buildStorage();
                    //Create the insert and execute
                    Insert insert = storage.objects().insert("mybucket", st);
                    insert.execute();
                } catch (IOException e) {
                    log.severe(e.getMessage());
                }
            }

这是我根据 REST API 的 ACL 条目:

 "kind": "storage#bucketAccessControls",
 "items": [
  {
   "kind": "storage#bucketAccessControl",
   "id": "gammeprediction/allUsers",
   "selfLink": "https://www.googleapis.com/storage/v1beta1/b/gammeprediction/acl/allUsers",
   "bucket": "mybucket",
   "entity": "allUsers",
   "role": "OWNER"
  }]

这就是我获得证书的方式:

private static Credential authorize() {
        GoogleCredential credential = null;
        //load properties
        Properties appProperties = new Properties();
            appProperties.load(RequestBuilder.class
                    .getResourceAsStream("/app.properties"));

        // creates an authorization with the key and service account given
        InputStream is = RequestBuilder.class.getResourceAsStream("/"
                + appProperties.getProperty("app.keyFileName"));
        PrivateKey pk;
        try {
            pk = PrivateKeys.loadFromKeyStore(KeyStore.getInstance("PKCS12"),
                    is, "notasecret", "privatekey", "notasecret");
            credential = new GoogleCredential.Builder()
                    .setTransport(HTTP_TRANSPORT)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(
                            appProperties
                                    .getProperty("app.serviceAccount"))
                    .setServiceAccountPrivateKey(pk)
                    .setServiceAccountScopes(PredictionScopes.PREDICTION,
                            DriveScopes.DRIVE, StorageScopes.DEVSTORAGE_FULL_CONTROL).build();


        return credential;
    }

存储桶上的权限是所有用户的 OWNER,但我仍然收到 403 Forbidden “Access not configured”错误。可能有什么问题?

【问题讨论】:

  • 当您说 OWNER 时,您是指 FULL_CONTROL 吗?我相信owner 会自动设置为项目所有者的组 ID。 (见developers.google.com/storage/docs/accesscontrol)你能分享桶上的实际ACL吗?您可以通过gsutil getacl gs://mybucket 获得此信息。
  • 要尝试的另一件事是使用 gsutil 进行上传测试以确保您的权限正确。如果有效,我们可以关注java代码,如果无效,我们应该关注权限。
  • 我不使用 gsutil 而是使用 api explorer 来设置 ACL。我会尝试使用 gsutil,但我认为它设置正确。
  • 我也可以使用 Java API 写入存储桶,但我想迁移到 REST API 以实现互操作性

标签: java google-cloud-storage


【解决方案1】:

一旦 JSON API 普遍可用,这个逻辑就会起作用。

不过,目前 JSON API 处于有限预览阶段。由于未知用户不被视为受限预览版的成员,因此目前无法通过 REST API 进行完全匿名的查询。相反,您必须在连接时至少提供一个列入白名单的 API 密钥。如果您没有提供更多身份信息,您将被视为匿名用户。或者,更进一步,您可以使用 OAuth2 凭据而不是被视为注册用户。更多内容见:https://developers.google.com/storage/docs/json_api/

那是一个 GWT RequestBuilder 吗?不幸的是,我并不完全熟悉它的用途。如果有帮助,这里有一个使用 Google API Java 客户端与 API 密钥建立连接的示例:https://code.google.com/p/google-api-java-client/wiki/OAuth2#Unauthenticated_access

此外,您对 setData() 的调用似乎传递了一个非 base64 的字符串,这将失败。

【讨论】:

  • 我正在使用服务帐户来访问它。这适用于我正在使用的其他 API(预测和驱动)。不过会尝试 Base64 ......我正在添加 requestbuilder 代码
  • 好的,经过一些更正后,我看到“访问未配置”消息,我想这意味着我无法访问受限预览...我会请求它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多