הפעלה של הגדרות שמירת אובייקטים ושימוש בהן

סקירה כללית

בדף הזה מוסבר איך להשתמש בתכונה 'נעילת שמירת אובייקטים', כולל הפעלת התכונה בקטגוריה והגדרת הגדרות שמירה לאובייקטים בקטגוריה.

התפקידים הנדרשים

כדי לקבל את ההרשאות שנדרשות להפעלת התכונה 'נעילת שמירת אובייקטים' בקטגוריה ולהגדרת תצורות שמירה באובייקטים, צריך לבקש מהאדמין להקצות לכם את תפקיד ה-IAM 'אדמין לניהול אחסון' (roles/storage.admin) בקטגוריה או בפרויקט שמכיל את הקטגוריה. התפקיד המוגדר מראש הזה כולל את ההרשאות שנדרשות להגדרה ולניהול של הגדרות שמירה. כדי לראות את ההרשאות הנדרשות, מרחיבים את הקטע ההרשאות הנדרשות:

ההרשאות הנדרשות

  • storage.buckets.create
  • storage.buckets.enableObjectRetention
  • storage.buckets.get
  • storage.buckets.list
    • ההרשאה הזו נדרשת רק אם אתם מתכננים להשתמש בGoogle Cloud מסוף כדי לבצע את ההוראות שבדף הזה.
  • storage.objects.get
  • storage.objects.list
    • ההרשאה הזו נדרשת רק אם אתם מתכננים להשתמש בGoogle Cloud מסוף כדי לבצע את ההוראות שבדף הזה.
  • storage.objects.overrideUnlockedRetention
    • ההרשאה הזו נדרשת רק אם אתם מתכננים לנעול או לקצר הגדרת שמירת נתונים קיימת.
  • storage.objects.setRetention
  • storage.objects.update

יכול להיות שתוכלו לקבל את ההרשאות האלה גם באמצעות תפקידים בהתאמה אישית.

במאמר הגדרה וניהול של מדיניות IAM בקטגוריות מוסבר איך מקצים תפקידים בקטגוריות. במאמר ניהול הגישה לפרויקטים מוסבר איך להקצות תפקידים בפרויקטים.

הפעלת שמירת אובייקטים בקטגוריה

כדי לאפשר הגדרות שימור לאובייקטים בקטגוריה: אם רוצים להפעיל הגדרות של שמירת אובייקטים בקטגוריה קיימת, צריך לפעול לפי ההוראות לשימוש במסוף Google Cloud .

המסוף

כדי להפעיל הגדרות של שמירת אובייקטים בקטגוריה חדשה:

  1. יוצרים מאגר כרגיל, ובשלב Choose how to protect object data בוחרים באפשרות Retention (For compliance) ואז באפשרות Enable object retention.

כדי להפעיל הגדרות של שמירת אובייקטים בקטגוריה קיימת:

  1. במסוף Google Cloud , נכנסים לדף Buckets של Cloud Storage.

    כניסה לדף Buckets

  2. ברשימת הקטגוריות, לוחצים על שם הקטגוריה שרוצים להפעיל בה שמירת אובייקטים.

  3. בוחרים בכרטיסייה Protection ליד החלק העליון של הדף.

    הסטטוס של שמירת האובייקטים בקטגוריה מוצג בקטע Object retention.

  4. בקטע Object retention, לוחצים על Object Retention Not Enabled.

  5. בתיבת הדו-שיח שמופיעה, לוחצים על Confirm.

שורת הפקודה

יוצרים קטגוריה כרגיל, וכוללים את הדגל --enable-per-object-retention בפקודה.

ספריות לקוח

C++

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage C++ API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

//! [create-bucket-with-object-retention]
namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name,
   std::string const& project_id) {
  auto bucket = client.CreateBucket(bucket_name, gcs::BucketMetadata{},
                                    gcs::EnableObjectRetention(true),
                                    gcs::OverrideDefaultProject(project_id));
  if (!bucket) throw std::move(bucket).status();

  if (!bucket->has_object_retention()) {
    throw std::runtime_error("missing object retention in new bucket");
  }
  std::cout << "Successfully created bucket " << bucket_name
            << " with object retention: " << bucket->object_retention()
            << "\n";
}
//! [create-bucket-with-object-retention]

C#

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage C# API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.


using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;

public class CreateBucketWithObjectRetentionSample
{
    public Bucket CreateBucketWithObjectRetention(
        string projectId = "your-project-id",
        string bucketName = "your-unique-bucket-name")
    {
        var storage = StorageClient.Create();
        var bucket = storage.CreateBucket(projectId, bucketName, new CreateBucketOptions
        {
            ObjectRetentionEnabled = true
        });
        Console.WriteLine($"Created {bucketName}, with Object Retention enabled.");
        return bucket;
    }
}

Java

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage Java API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.


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

public class CreateBucketWithObjectRetention {
  public static void createBucketWithObjectRetention(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID to give your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

    Bucket bucket =
        storage.create(
            BucketInfo.of(bucketName), Storage.BucketTargetOption.enableObjectRetention(true));

    System.out.println(
        "Created bucket "
            + bucket.getName()
            + " with object retention enabled setting: "
            + bucket.getObjectRetention().getMode().toString());
  }
}

Node.js

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage Node.js API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

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

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

// Creates a client
// The bucket in the sample below will be created in the project associated with this client.
// For more information, please see https://cloud.google.com/docs/authentication/production or https://googleapis.dev/nodejs/storage/latest/Storage.html
const storage = new Storage();

async function createBucketWithObjectRetention() {
  const [bucket] = await storage.createBucket(bucketName, {
    enableObjectRetention: true,
  });

  console.log(
    `Created '${bucket.name}' with object retention enabled setting: ${bucket.metadata.objectRetention.mode}`
  );
}

createBucketWithObjectRetention().catch(console.error);

PHP

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage PHP API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

use Google\Cloud\Storage\StorageClient;

/**
 * Create a Cloud Storage bucket with the object retention enabled.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 */
function create_bucket_with_object_retention(string $bucketName): void
{
    $storage = new StorageClient();

    $bucket = $storage->createBucket($bucketName, [
        'enableObjectRetention' => true
    ]);
    printf(
        'Created bucket %s with object retention enabled setting: %s' . PHP_EOL,
        $bucketName,
        $bucket->info()['objectRetention']['mode']
    );
}

Python

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage Python API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

from google.cloud import storage


def create_bucket_object_retention(bucket_name):
    """Creates a bucket with object retention enabled."""
    # The ID of your GCS bucket
    # bucket_name = "your-bucket-name"

    storage_client = storage.Client()
    bucket = storage_client.create_bucket(bucket_name, enable_object_retention=True)

    print(f"Created bucket {bucket_name} with object retention enabled setting: {bucket.object_retention_mode}")

Rust

use google_cloud_storage::{client::StorageControl, model::Bucket, model::bucket::ObjectRetention};

pub async fn sample(
    client: &StorageControl,
    project_id: &str,
    bucket_id: &str,
) -> anyhow::Result<()> {
    let bucket = client
        .create_bucket()
        .set_parent("projects/_")
        .set_bucket_id(bucket_id)
        .set_bucket(
            Bucket::new()
                .set_project(format!("projects/{project_id}"))
                .set_object_retention(ObjectRetention::new().set_enabled(true)),
        )
        .send()
        .await?;
    println!("successfully created bucket {bucket:?}");
    Ok(())
}

ממשקי API ל-REST

‫API בפורמט JSON

יוצרים קטגוריה כרגיל, וכוללים את פרמטר השאילתה enableObjectRetention=true כחלק מהבקשה.

‫API בפורמט XML

יוצרים קטגוריה כרגיל, וכוללים את הכותרת x-goog-bucket-object-lock-enabled: True כחלק מהבקשה.

צפייה בסטטוס שמירת האובייקטים של קטגוריה

כדי לבדוק אם אפשר להגדיר הגדרות שמירה לאובייקטים בקטגוריה:

המסוף

  1. במסוף Google Cloud , נכנסים לדף Buckets של Cloud Storage.

    כניסה לדף Buckets

  2. לוחצים על שם הקטגוריה שרוצים לבדוק את הסטטוס שלה.

  3. לוחצים על הכרטיסייה הגנה.

  4. הסטטוס של שמירת האובייקטים בקטגוריה מוצג בקטע Object retention.

שורת הפקודה

משתמשים בפקודה gcloud storage buckets describe עם הדגל --format:

gcloud storage buckets describe gs://BUCKET_NAME --format="default(per_object_retention)"

כאשר BUCKET_NAME הוא השם של הקטגוריה שבמדיניות שמירת הנתונים שלה רוצים לצפות. לדוגמה, my-bucket.

אם לקטגוריה יש מדיניות שמירת נתונים שהוגדרה בהצלחה, התשובה תיראה דומה לזו:

per_object_retention:
  mode: Enabled

אם לקטגוריה אין מדיניות שמירת נתונים שהוגדרה בהצלחה, התשובה תיראה דומה לזו:

null

ספריות לקוח

C++

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage C++ API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

כדי לראות את הגדרות השמירה של אובייקטים בקטגוריה, פועלים לפי ההוראות להצגת המטא-נתונים של הקטגוריה ומחפשים את השדה של שמירת האובייקט בתשובה.
namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  StatusOr<gcs::BucketMetadata> bucket_metadata =
      client.GetBucketMetadata(bucket_name);
  if (!bucket_metadata) throw std::move(bucket_metadata).status();

  std::cout << "The metadata for bucket " << bucket_metadata->name() << " is "
            << *bucket_metadata << "\n";
}

C#

למידע נוסף, קראו את מאמרי העזרה של Cloud Storage C# API.

כדי לבצע אימות ב-Cloud Storage, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

כדי לראות את הגדרות השמירה של אובייקטים בקטגוריה, פועלים לפי ההוראות להצגת המטא-נתונים של הקטגוריה ומחפשים את השדה של שמירת האובייקט בתשובה.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;

public class GetBucketMetadataSample
{
    public Bucket GetBucketMetadata(string bucketName = "your-unique-bucket-name")
    {
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName, new GetBucketOptions { Projection = Projection.Full });
        Console.WriteLine($"Bucket:\t{bucket.Name}");
        Console.WriteLine($"Acl:\t{bucket.Acl}");
        Console.WriteLine($"Billing:\t{bucket.Billing}");
        Console.WriteLine($"Cors:\t{bucket.Cors}");
        Console.WriteLine($"DefaultEventBasedHold:\t{bucket.DefaultEventBasedHold}");
        Console.WriteLine($"DefaultObjectAcl:\t{bucket.DefaultObjectAcl}");
        Console.WriteLine($"Encryption:\t{bucket.Encryption}");
        if (bucket.Encryption != null)
        {
            Console.WriteLine($"KmsKeyName:\t{bucket.Encryption.DefaultKmsKeyName}");
        }
        Console.WriteLine($"Id:\t{bucket.Id}");
        Console.WriteLine($"Kind:\t{bucket.Kind}");
        Console.WriteLine($"Lifecycle:\t{bucket.Lifecycle}");
        Console.WriteLine($"Location:\t{bucket.Location}");
        Console.WriteLine($"LocationType:\t{bucket.LocationType}");
        Console.WriteLine($"Logging:\t{bucket.Logging}");
        Console.WriteLine($"Metageneration:\t{bucket.Metageneration}");
        Console.WriteLine($"ObjectRetention:\t{bucket.ObjectRetention}");
        Console.WriteLine($"Owner:\t{bucket.Owner}");
        Console.WriteLine($"ProjectNumber:\t{bucket.ProjectNumber}");
        Console.WriteLine($"RetentionPolicy:\t{bucket.RetentionPolicy}");
        Console.WriteLine($"SelfLink:\t{bucket.SelfLink}");
        Console.WriteLine($"StorageClass:\t{bucket.StorageClass}");
        Console.WriteLine($"TimeCreated:\t{bucket.TimeCreated}");
        Console.WriteLine($"Updated:\t{bucket.Updated}");
        Console.WriteLine($"Versioning:\t{bucket.Versioning}");
        Console.WriteLine($"Website:\t{bucket.Website}");
        Console.WriteLine($"TurboReplication:\t{bucket.Rpo}");
        if (bucket.Labels != null)
        {
            Console.WriteLine("Labels:");
            foreach (var