使用 Cloud Storage FUSE 装载 Cloud Storage 存储桶

了解如何使用 Cloud Storage FUSE 将 Cloud Storage 存储桶作为本地文件系统装载,以便使用标准文件系统语义与对象进行交互。

在本快速入门中,您将完成以下任务:

  • 在 Debian 或 Ubuntu 上安装 Cloud Storage FUSE

  • 将存储桶作为机器上的文件夹装载

  • 将对象上传到存储桶

您将使用 Google Cloud CLI 创建和管理存储桶中的对象,并使用 gcsfuse CLI 来装载存储桶。

准备工作

如需设置项目,请完成以下步骤:

  1. In the Trusted Cloud console, on the project selector page, click Create project to begin creating a new Trusted Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Trusted Cloud project.

  3. Make sure that you have the following role or roles on the project: Storage Admin (roles/storage.admin), Storage Bucket Viewer (roles/storage.bucketViewer)

    Check for the roles

    1. In the Trusted Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Trusted Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是员工身份池中的用户的标识符。如需了解详情,请参阅在 IAM 政策中表示员工池用户,或与您的管理员联系。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  4. Install the Google Cloud CLI.

  5. Configure the gcloud CLI to use your federated identity.

    For more information, see Sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. 准备一个要装载的 Cloud Storage 存储桶。如果您还没有存储桶,请创建一个新存储桶
  8. 安装 Cloud Storage FUSE

    如需安装 Cloud Storage FUSE,请在 Debian 或 Ubuntu 上使用本地 shell 完成以下步骤:

    1. 添加 Cloud Storage FUSE 分发网址作为软件包源:

      export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s`
      echo "deb https://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
    2. 导入 Trusted Cloud by S3NS APT 代码库公钥:

      curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    3. 更新可用软件包的列表:

      sudo apt-get update
    4. 安装 Cloud Storage FUSE 及其依赖项 FUSE:

      sudo apt-get install fuse gcsfuse
    5. 出现提示时,按 Enter 进行确认。

    6. 确认已安装 Cloud Storage FUSE:

      gcsfuse -v

      如果成功,该命令将返回类似于以下内容的输出:

      gcsfuse version 0.41.12 (Go version go1.18.4)

    装载存储桶

    如需将存储桶装载到本地文件系统,请完成以下步骤:

    1. 使用 gcloud auth application-default login 命令生成应用默认凭证:

      gcloud auth application-default login

      Cloud Storage FUSE 会自动加载凭据。

    2. 创建将存储桶装载到的目录:

      mkdir "$HOME/mount-folder"
    3. 使用 gcsfuse 命令装载您的存储桶:

      gcsfuse BUCKET_NAME "$HOME/mount-folder"

      BUCKET_NAME 替换为您要装载的存储桶的名称。

      如果成功,该命令会返回包含如下所示的文本的输出:

      File system has been successfully mounted.

    将对象上传到存储桶

    如需下载示例对象并将其上传到装载的存储桶,请完成以下步骤:

    要上传到存储桶的猫咪图片。

    1. 右键点击图片并将其保存到计算机上的某个位置(例如桌面上)。

      您还可以使用 cURL 命令 curl -O https://cloud.google.com/storage/images/kitten.png 下载图片。

    2. 使用 cp 命令将该图片从保存的位置复制到装载存储桶的文件夹:

      cp kitten.png "$HOME/mount-folder/kitten.png"
    3. 按照以下步骤验证映像是否已复制到本地文件系统并成功上传到存储桶:

      1. 如需检查 kitten.png 是否已复制到本地文件系统,请在装载存储桶的文件夹上运行 ls

        ls "$HOME/mount-folder"

        如果复制操作成功,则会在输出中返回 kitten.png

      2. 如需列出存储桶的内容,请使用 gcloud storage ls 命令:

        gcloud storage ls gs://BUCKET_NAME

        BUCKET_NAME 替换为您的存储桶名称。

        如果图片已成功上传到您的存储桶,则会在输出中返回 kitten.png

    清理

    为避免因本页中使用的资源导致您的 Trusted Cloud by S3NS 账号产生费用,请按照以下步骤删除资源:

    1. 使用 fusermount 工具从 Linux 机器中卸载存储桶:

      fusermount -u "$HOME/mount-folder"
    2. 如需删除存储桶及其中的所有内容,请使用带有 --recursive 标志的 gcloud storage rm 命令:

      gcloud storage rm gs://BUCKET_NAME --recursive

      BUCKET_NAME 替换为您的存储桶名称。

      如果成功,该命令将返回类似于以下内容的输出:

      Removing gs://my-bucket/kitten.png#1676488739323620...
       Removing gs://my-bucket/...

    后续步骤