创建和管理文件夹

本页面介绍了如何在启用了分层命名空间的存储桶中创建、列出、上传和删除文件夹以及获取其元数据。

准备工作

确保您的存储桶启用了分层命名空间。如需详细了解如何为存储桶启用分层命名空间,请参阅创建启用了分层命名空间的存储桶

创建文件夹

本部分介绍如何创建文件夹。

控制台

  1. 在 Google Cloud 控制台中,转到 Cloud Storage 存储桶页面。

    进入“存储桶”

  2. 在存储桶列表中,点击要在其中创建文件夹的存储桶的名称。
  3. 存储桶详情页面中,点击创建文件夹以创建一个空文件夹。
  4. 名称字段中,为文件夹输入名称。如需了解命名注意事项,请参阅注意事项
  5. 点击创建

    新创建的文件夹会显示在文件夹浏览器窗格中。

命令行

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 在开发环境中,运行 gcloud storage folders create 命令:

    gcloud storage folders create --recursive gs://BUCKET_NAME/FOLDER_NAME

    其中:

    • BUCKET_NAME 是您的存储桶的名称。 例如 my-bucket
    • FOLDER_NAME 是要创建的文件夹的名称。例如 my-folder/。如需了解文件夹名称,请参阅文件夹概览文档
    • --recursive 是一个标志,用于自动创建文件夹以及所有不存在的父级文件夹。如果父级文件夹已存在,则此设置是可选的。

    如果请求成功,该命令将返回以下消息:

    Completed 1/1
  3. 客户端库

    C++

    如需了解详情,请参阅 Cloud Storage C++ API 参考文档

    如需向 Cloud Storage 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

    namespace storagecontrol = google::cloud::storagecontrol_v2;
    [](storagecontrol::StorageControlClient client,
       std::string const& bucket_name, std::string const& folder_id) {
      auto const parent = std::string{"projects/_/buckets/"} + bucket_name;
      auto folder = client.CreateFolder(
          parent, google::storage::control::v2::Folder{}, folder_id);
      if (!folder) throw std::move(folder).status();
    
      std::cout << "Created folder: " << folder->name() << "\n";
    }

    C#

    如需了解详情,请参阅 Cloud Storage C# API 参考文档

    如需向 Cloud Storage 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

    using Google.Cloud.Storage.Control.V2;
    using System;
    
    public class StorageControlCreateFolderSample
    {
        public Folder StorageControlCreateFolder(string bucketName = "your-unique-bucket-name",
            string folderName = "your_folder_name")
        {
            StorageControlClient storageControl = StorageControlClient.Create();
    
            var request = new CreateFolderRequest
            {
                // Set project to "_" to signify globally scoped bucket
                Parent = BucketName.FormatProjectBucket("_", bucketName),
                FolderId = folderName
            };
    
            Folder folder = storageControl.CreateFolder(request);
    
            Console.WriteLine($"Folder {folderName} created in bucket {bucketName}");
            return folder;
        }
    }

    Go

    如需了解详情,请参阅 Cloud Storage Go API 参考文档

    如需向 Cloud Storage 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"
    
    	control "cloud.google.com/go/storage/control/apiv2"
    	"cloud.google.com/go/storage/control/apiv2/controlpb"
    )
    
    // createFolder creates a folder in the bucket with the given name.
    func createFolder(w io.Writer