刪除物件

本頁面說明如何從 Cloud Storage 的值區刪除物件。如需物件總覽,請參閱重要詞彙一文。

如要從一個 Cloud Storage 值區刪除物件,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中點選含有您要刪除物件的值區名稱。

  3. 在「Objects」(物件) 分頁中,瀏覽到物件。

  4. 勾選您要刪除之物件旁的核取方塊。

  5. 按一下 [Delete] (刪除) 按鈕。

  6. 按一下出現的對話框中的 [Delete] (刪除)

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
需要協助嗎?請前往我們的支援網頁