使用 gcloud 工具发现对象存储空间
本页面介绍了如何使用 gcloud 命令行工具执行 Cloud Storage 中的基本任务。
Cloud Storage 将按您使用的资源向您收费。在本快速入门中,所使用 Cloud Storage 资源的价值通常不到 $0.01 USD。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith a name for the Google Cloud project you are creating.
- 
        Select the Google Cloud project that you created: gcloud config set project PROJECT_ID Replace PROJECT_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
    
        Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.admingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE Replace the following: - PROJECT_ID: Your project ID.
- USER_IDENTIFIER: The identifier for your user account. For example,- myemail@example.com.
- ROLE: The IAM role that you grant to your user account.
 
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith a name for the Google Cloud project you are creating.
- 
        Select the Google Cloud project that you created: gcloud config set project PROJECT_ID Replace PROJECT_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
    
        Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.admingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE Replace the following: - PROJECT_ID: Your project ID.
- USER_IDENTIFIER: The identifier for your user account. For example,- myemail@example.com.
- ROLE: The IAM role that you grant to your user account.
 
- 打开一个终端窗口。
- 使用 - gcloud storage buckets create命令创建存储桶并为其指定一个唯一的名称:- gcloud storage buckets create gs://my-awesome-bucket/ --uniform-bucket-level-access - 在上述命令中,使用了名为“my-awesome-bucket”的存储桶。您必须选择您自己的全局唯一存储桶名称。 - 如果成功,此命令会返回以下内容: - Creating gs://my-awesome-bucket/... 
- 右键点击上面的图片并将其保存到计算机上的某个位置(例如桌面上)。 
- 使用 - gcloud storage cp命令将该图片从保存位置复制到您创建的存储桶中:- gcloud storage cp Desktop/kitten.png gs://my-awesome-bucket - 如果成功,此命令会返回以下内容: - Copying file://Desktop/kitten.png [Content-Type=image/png]... Uploading gs://my-awesome-bucket/kitten.png: 0 B/164.3 KiB Uploading gs://my-awesome-bucket/kitten.png: 164.3 KiB/164.3 KiB - 您刚刚在存储分区中存储了一个对象。 
- 使用 - gcloud storage cp命令将存储在存储桶中的图片下载到计算机上的某个位置(例如桌面上):- gcloud storage cp gs://my-awesome-bucket/kitten.png Desktop/kitten2.png - 如果成功,此命令会返回以下内容: - Copying gs://my-awesome-bucket/kitten.png... Downloading file://Desktop/kitten2.png: 0 B/164.3 KiB Downloading file://Desktop/kitten2.png: 164.3 KiB/164.3 KiB - 您刚刚从存储分区中下载了某个内容。 
- 使用 - gcloud storage cp命令创建文件夹并将该图片复制到该文件夹中:- gcloud storage cp gs://my-awesome-bucket/kitten.png gs://my-awesome-bucket/just-a-folder/kitten3.png - 如果成功,此命令会返回以下内容: - Copying gs://my-awesome-bucket/kitten.png [Content-Type=image/png]... Copying ...my-awesome-bucket/just-a-folder/kitten3.png: 164.3 KiB/164.3 KiB - 您刚刚将图片复制到存储桶内的新文件夹中。 
- 使用 - gcloud storage ls命令列出存储桶顶层的内容:- gcloud storage ls gs://my-awesome-bucket - 如果成功,该命令将返回类似于以下内容的消息: - gs://my-awesome-bucket/kitten.png gs://my-awesome-bucket/just-a-folder/ - 您刚刚看到了存储桶顶层的内容。 
- 使用 - gcloud storage ls命令和- --long标志来获取图片之一的一些相关详情:- gcloud storage ls gs://my-awesome-bucket/kitten.png --long - 如果成功,该命令将返回类似于以下内容的消息: - 2638 2016-02-26T23:05:14Z gs://my-awesome-bucket/kitten.png TOTAL: 1 objects, 168243.2 bytes (164.3 KiB) - 您刚刚获得了有关图片大小和创建日期的信息。 
- 使用 - gcloud storage buckets add-iam-policy-binding命令向所有用户授予读取您存储桶中存储的图片的权限:- gcloud storage buckets add-iam-policy-binding gs://my-awesome-bucket --member=allUsers --role=roles/storage.objectViewer - 如果响应包含以下内容,则表示此命令已成功执行: - bindings: - members: - allUsers role: roles/storage.objectViewer- 现在,任何人都可以获取您的图片。 
- 如需移除此访问权限,请使用以下命令: - gcloud storage buckets remove-iam-policy-binding gs://my-awesome-bucket --member=allUsers --role=roles/storage.objectViewer - 如果未返回错误,则表示此命令已成功执行。 - 您已移除对您存储桶中图片的公开访问权限。 
- 使用 - gcloud storage buckets add-iam-policy-binding命令向特定电子邮件地址授予向存储桶添加对象的权限:- gcloud storage buckets add-iam-policy-binding gs://my-awesome-bucket --member=user:jeffersonloveshiking@gmail.com --role=roles/storage.objectCreator - 如果响应包含以下内容,则表示此命令已成功执行: - bindings: - members: - user:jeffersonloveshiking@gmail.com role: roles/storage.objectCreator- 现在,其他人就可以向您的存储桶添加内容了。 
- 要移除此权限,请使用以下命令: - gcloud storage buckets remove-iam-policy-binding gs://my-awesome-bucket --member=user:jeffersonloveshiking@gmail.com --role=roles/storage.objectCreator - 如果未返回错误,则表示此命令已成功执行。 - 您已移除用户在此存储分区上的访问权限。 
- 使用 - gcloud storage rm命令删除图片之一:- gcloud storage rm gs://my-awesome-bucket/kitten.png - 如果成功,此命令会返回以下内容: - Removing gs://my-awesome-bucket/kitten.png... - 图片的这一副本不再存储在 Cloud Storage 中(尽管您在文件夹 - just-a-folder/中创建的副本仍然存在)。
- 打开终端窗口(如果尚未打开)。
- 使用带有 - --recursive标志的- gcloud storage rm命令删除存储桶以及其内的所有内容:- gcloud storage rm gs://my-awesome-bucket --recursive - 如果成功,该命令将返回类似于以下内容的消息: - Removing gs://my-awesome-bucket/just-a-folder/cloud-storage.logo.png#1456530077282000... Removing gs://my-awesome-bucket/... - 您的存储桶及其内容将被删除。 
- 了解 Cloud Storage 价格。
- 参阅相关参考页面以了解 Google Cloud CLI 命令,例如创建存储桶 (buckets create)、复制 (cp)、列出 (ls)、添加访问权限 (buckets add-iam-policy-binding) 和移除 (rm)。
创建存储桶
存储桶是 Cloud Storage 中用于存放数据的基本容器。
要创建存储桶,请执行以下操作:
您刚刚创建了一个存储桶,现在可以在其中存储您的数据了!
Creating gs://my-awesome-bucket/... ServiceException: 409 Bucket my-awesome-bucket already exists.
请使用其他存储分区名称重试。
将对象上传到存储桶

从存储桶下载对象
将对象复制到存储桶中的文件夹
列出存储桶或文件夹的内容
列出对象的详细信息
将对象设置为可公开访问
向某人授予您的存储桶的访问权限
删除对象
清理
为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。