Menghapus notifikasi Pub/Sub

Menghapus notifikasi Pub/Sub dari bucket penyimpanan Cloud Storage.

Pelajari lebih lanjut

Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat referensi berikut:

Contoh kode

C++

Untuk informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage C++ API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

namespace gcs = ::google::cloud::storage;
[](gcs::Client client, std::string const& bucket_name,
   std::string const& notification_id) {
  google::cloud::Status status =
      client.DeleteNotification(bucket_name, notification_id);
  if (!status.ok()) throw std::runtime_error(status.message());

  std::cout << "Successfully deleted notification " << notification_id
            << " on bucket " << bucket_name << "\n";
}

C#

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage C# API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


using System;
using Google.Cloud.Storage.V1;

public class DeletePubSubNotificationSample
{
    public void DeletePubSubNotification(
        string bucketName = "your-unique-bucket-name",
        string notificationId = "notificationId")
    {
        StorageClient storage = StorageClient.Create();
        storage.DeleteNotification(bucketName, notificationId);

        Console.WriteLine("Successfully deleted notification with ID " + notificationId + " for bucket " + bucketName);
    }
}

Go

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage Go API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/storage"
)

// deleteBucketNotification deletes a notification configuration for a bucket.
func deleteBucketNotification(w io.Writer, bucketName, notificationID string) error {
	// bucketName := "bucket-name"
	// notificationID := "notification-id"

	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %w", err)
	}
	defer client.Close()

	bucket := client.Bucket(bucketName)

	if err := bucket.DeleteNotification(ctx, notificationID); err != nil {
		return fmt.Errorf("Bucket.DeleteNotification: %w", err)
	}
	fmt.Fprintf(w, "Successfully deleted notification with ID %s for bucket %s.\n", notificationID, bucketName)
	return nil
}

Java

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage Java API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class DeleteBucketPubSubNotification {

  public static void deleteBucketPubSubNotification(String bucketName, String notificationId) {
    // The ID to give your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    // The NotificationId for the notification you would like to delete
    // String notificationId = "your-unique-notification-id"

    Storage storage = StorageOptions.newBuilder().build().getService();
    boolean success = storage.deleteNotification(bucketName, notificationId);
    if (success) {
      System.out.println("Successfully deleted notification");
    } else {
      System.out.println("Failed to find notification");
    }
  }
}

Node.js

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage Node.js API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The ID of the notification
// const notificationId = '1';

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

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

async function deleteNotification() {
  // Deletes the notification from the bucket
  await storage.bucket(bucketName).notification(notificationId).delete();

  console.log(`Notification ${notificationId} deleted.`);
}

deleteNotification().catch(console.error);

PHP

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage PHP API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

use Google\Cloud\Storage\StorageClient;

/**
 * Deletes a notification configuration for a bucket.
 * This sample is used on this page:
 *   https://cloud.google.com/storage/docs/reporting-changes
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 *        (e.g. 'your-bucket')
 * @param string $notificationId The ID of the notification.
 *        (e.g. 'your-notification-id')
 */
function delete_bucket_notifications(
    string $bucketName,
    string $notificationId
): void {
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $notification = $bucket->notification($notificationId);
    $notification->delete();

    printf(
        'Successfully deleted notification with ID %s for bucket %s' . PHP_EOL,
        $notification->id(),
        $bucketName
    );
}

Python

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage Python API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import storage

def delete_bucket_notification(bucket_name, notification_id):
    """Deletes a notification configuration for a bucket."""
    # The ID of your GCS bucket
    # bucket_name = "your-bucket-name"
    # The ID of the notification
    # notification_id = "your-notification-id"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    notification = bucket.notification(notification_id=notification_id)
    notification.delete()

    print(f"Successfully deleted notification with ID {notification_id} for bucket {bucket_name}")

Ruby

Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi Cloud Storage Ruby API.

Untuk melakukan autentikasi ke Cloud Storage, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

require "google/cloud/storage"

def delete_bucket_notification bucket_name:, notification_id:
  # The ID of your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  # The ID of your notification configured for the bucket
  # notification_id = "your-notification-id"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name
  notification = bucket.notification notification_id
  notification.delete

  puts "Successfully deleted notification with ID #{notification_id} for bucket #{bucket_name}"
end

Langkah berikutnya

Guna menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.