删除对象

本页面介绍了如何从 Cloud Storage 中的存储分区删除对象。如需查看对象的概览,请参阅关键术语

要从其中一个 Cloud Storage 存储分区删除对象,请执行以下操作:

Console

  1. 在 Google Cloud Platform Console 中打开 Cloud Storage 浏览器。
    打开 Cloud Storage 浏览器
  2. 在存储分区列表中,点击包含要删除的对象的存储分区的名称。

  3. 对象标签中,导航到该对象上。

  4. 点击要删除的对象旁边的复选框。

  5. 点击删除按钮。

  6. 在出现的对话框中点击删除

gsutil

使用 gsutil rm 命令,并将 [VALUES_IN_BRACKETS] 替换为适当的值:

gsutil rm gs://[BUCKET_NAME]/[OBJECT_NAME]

如果成功,则响应类似如下示例:

Operation completed over 1 objects.

代码示例

C++

如需了解更多信息,请参阅 Cloud Storage C++ API 参考文档

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  google::cloud::Status status =
      client.DeleteObject(bucket_name, object_name);

  if (!status.ok()) {
    throw std::runtime_error(status.message());
  }

  std::cout << "Deleted " << object_name << " in bucket " << bucket_name
            << "\n";
}

C#

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

private void DeleteObject(string bucketName, IEnumerable<string> objectNames)
{
    var storage = StorageClient.Create();
    foreach (string objectName in objectNames)
    {
        storage.DeleteObject(bucketName, objectName);
        Console.WriteLine($"Deleted {objectName}.");
    }
}

Go

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

o := client.Bucket(bucket).Object(object)
if err := o.Delete(ctx); err != nil {
	return err
}

Java

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

BlobId blobId = BlobId.of(bucketName, blobName);
boolean deleted = storage.delete(blobId);
if (deleted) {
  // the blob was deleted
} else {
  // the blob was not found
}

Node.js

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

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to delete, e.g. file.txt';

// Deletes the file from the bucket
await storage
  .bucket(bucketName)
  .file(filename)
  .delete();

console.log(`gs://${bucketName}/${filename} deleted.`);

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Delete an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 * @param array $options
 *
 * @return void
 */
function delete_object($bucketName, $objectName, $options = [])
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->delete();
    printf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
}

Python

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

def delete_blob(bucket_name, blob_name):
    """Deletes a blob from the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    blob.delete()

    print('Blob {} deleted.'.format(blob_name))

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of file in Google Cloud Storage to delete"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.delete

puts "Deleted #{file.name}"

REST API

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 使用 cURL(附带 DELETE 请求)调用 JSON API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:

    curl -X DELETE \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]"

XML API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 使用 cURL(附带 DELETE Object 请求)调用 XML API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:

    curl -X DELETE \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    "https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]"

后续步骤

此页内容是否有用?请给出您的反馈和评价:

发送以下问题的反馈:

此网页
Cloud Storage
需要帮助?请访问我们的支持页面