从云外部使用 Hadoop 客户端访问 GCS

Accessing GCS with Hadoop client from outside of Cloud

我想通过 Hadoop 客户端访问 Google 云存储。我想在 Google 云之外的机器上使用它。

我遵循了 here 的指示。 我创建了服务帐户并生成了密钥文件。我还创建了 core-site.xml 文件并下载了必要的库。

然而,当我尝试 运行 简单的 hdfs dfs -ls gs://bucket-name 命令时,我得到的是:

Error getting access token from metadata server at: http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token

当我在内部 Google 云中执行此操作时,它可以工作,但尝试从外部连接到 GCS,上面显示错误。

如何通过Hadoop Client连接GCS?有可能吗?我没有到 169.254.169.254 地址的路由。

这是我的核心-site.xml(我在这个例子中更改了关键路径和电子邮件):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
  <property>
    <name>spark.hadoop.google.cloud.auth.service.account.enable</name>
    <value>true</value>
  </property>
  <property>
    <name>spark.hadoop.google.cloud.auth.service.account.json.keyfile</name>
    <value>path/to/key.json</value>
  </property>
  <property>
    <name>fs.gs.project.id</name>
    <value>ringgit-research</value>
    <description>
      Optional. Google Cloud Project ID with access to GCS buckets.
      Required only for list buckets and create bucket operations.
    </description>
  </property>
  <property>
    <name>fs.AbstractFileSystem.gs.impl</name>
    <value>com.google.cloud.hadoop.fs.gcs.GoogleHadoopFS</value>
    <description>The AbstractFileSystem for gs: uris.</description>
  </property>
  <property>
    <name>fs.gs.auth.service.account.email</name>
    <value>myserviceaccountaddress@google</value>
    <description>
      The email address is associated with the service account used for GCS
      access when fs.gs.auth.service.account.enable is true. Required
      when authentication key specified in the Configuration file (Method 1)
      or a PKCS12 certificate (Method 3) is being used.
    </description>
  </property>
</configuration>

可能是 hadoop 服务还没有更新你的 core-site.xml 文件,所以我的建议是重启 hadoop 的服务,你可以采取的另一个行动是检查访问控制选项[1].

如果您在采取建议的措施后仍然遇到同样的问题,请post完整的错误消息。

[1]https://cloud.google.com/storage/docs/access-control/

问题在于我尝试了错误的身份验证方法。使用的方法假定它在 google 云中 运行 并且它正在尝试连接到 google 元数据服务器。当 运行 在 google 之外时,由于显而易见的原因,它不起作用。

答案在这里:Migrating 50TB data from local Hadoop cluster to Google Cloud Storage在所选答案中有正确的核心-site.xml。

应使用

属性 fs.gs.auth.service.account.keyfile 而不是 spark.hadoop.google.cloud.auth.service.account.json.keyfile。唯一的区别是这个 属性 需要 p12 密钥文件而不是 json.