שימוש בכללי מדיניות שמירת נתונים ונעילה שלהם

סקירה כללית

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

לפני שמתחילים

לפני שמשתמשים בתכונה Bucket Lock, צריך לוודא שהשלמתם את השלבים שמפורטים בקטעים הבאים.

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

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

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

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

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

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

הגדרת מדיניות שמירת נתונים בקטגוריה

כדי להוסיף, לשנות או להסיר מדיניות שמירת נתונים בקטגוריה:

המסוף

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

    כניסה לדף Buckets

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

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

  4. בקטע Retention policy, מגדירים את מדיניות שמירת הנתונים:

    1. אם בקטגוריה לא פועלת כרגע מדיניות שמירת נתונים, לוחצים על הקישור Set Retention Policy. בוחרים יחידת זמן ואת משך הזמן של תקופת השמירה.

    2. אם בקטגוריה פועלת כרגע מדיניות שמירת נתונים, היא תופיע בחלק הזה. לוחצים על Edit כדי לשנות את זמן השמירה, או על Delete כדי להסיר לגמרי את מדיניות שמירת הנתונים.

    מידע על האופן שבוGoogle Cloud מסוף Google Cloud מבצע המרה בין יחידות זמן שונות מופיע במאמר תקופות שמירה.

במאמר פתרון בעיות מוסבר איך מקבלים מידע מפורט על שגיאות בנושא פעולות ב-Cloud Storage שנכשלו ב Google Cloud מסוף.

שורת הפקודה

משתמשים בפקודה gcloud storage buckets update עם הדגל המתאים:

gcloud storage buckets update gs://BUCKET_NAME FLAG

כאשר:

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

  • FLAG היא ההגדרה הרצויה לתקופת השמירה של הקטגוריה. משתמשים באחד מהפורמטים הבאים:

    • --retention-period ותקופת שמירה, אם רוצים להוסיף או לשנות מדיניות שמירת נתונים. לדוגמה, --retention-period=1d43200s.
    • --clear-retention-period, אם רוצים להסיר את מדיניות שמירת הנתונים מהקטגוריה.

אם הפעולה בוצעה ללא שגיאות, התשובה נראית כך:

Updating gs://my-bucket/...
  Completed 1  

ספריות לקוח

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,
   std::chrono::seconds period) {
  StatusOr<gcs::BucketMetadata> original =
      client.GetBucketMetadata(bucket_name);
  if (!original) throw std::move(original).status();

  StatusOr<gcs::BucketMetadata> patched = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetRetentionPolicy(period),
      gcs::IfMetagenerationMatch(original->metageneration()));
  if (!patched) throw std::move(patched).status();

  if (!patched->has_retention_policy()) {
    std::cout << "The bucket " << patched->name()
              << " does not have a retention policy set.\n";
    return;
  }

  std::cout << "The bucket " << patched->name()
            << " retention policy is set to " << patched->retention_policy()
            << "\n";
}

הדוגמה הבאה מסירה את מדיניות שמירת הנתונים מקטגוריה:

namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  StatusOr<gcs::BucketMetadata> original =
      client.GetBucketMetadata(bucket_name);
  if (!original) throw std::move(original).status();

  StatusOr<gcs::BucketMetadata> patched = client.PatchBucket(
      bucket_name, gcs::BucketMetadataPatchBuilder().ResetRetentionPolicy(),
      gcs::IfMetagenerationMatch(original->metageneration()));
  if (!patched) throw std::move(patched).status();

  if (!patched->has_retention_policy()) {
    std::cout << "The bucket " << patched->name()
              << " does not have a retention policy set.\n";
    return;
  }

  std::cout << "The bucket " << patched->name()
            << " retention policy is set to " << patched->retention_policy()
            << ". This is unexpected, maybe a concurrent change by another"
            << " application?\n";
}

C#

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

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

הדוגמה הבאה מגדירה מדיניות שמירת נתונים בקטגוריה:


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

public class SetRetentionPolicySample
{
    /// <summary>
    /// Sets the bucket's retention policy.
    /// </summary>
    /// <param name="bucketName">The name of the bucket.</param>
    /// <param name="retentionPeriod">The duration in seconds that objects need to be retained. The retention policy enforces a minimum retention
    /// time for all objects contained in the bucket, based on their creation time. Any
    /// attempt to overwrite or delete objects younger than the retention period will
    /// result in a PERMISSION_DENIED error. An unlocked retention policy can be modified
    /// or removed from the bucket via a storage.buckets.update operation. A locked retention
    /// policy cannot be removed or shortened in duration for the lifetime of the bucket.
    /// Attempting to remove or decrease the period of a locked retention policy will result
    /// in a PERMISSION_DENIED error.</param>
    public RetentionPolicyData SetRetentionPolicy(
        string bucketName = "your-unique-bucket-name",
        long retentionPeriod = 10)
    {
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName);
        bucket.RetentionPolicy = new RetentionPolicyData { RetentionPeriod = retentionPeriod };

        bucket = storage.UpdateBucket(bucket);

        Console.WriteLine($"Retention policy for {bucketName} was set to {retentionPeriod}");
        return bucket.RetentionPolicy;
    }
}

הדוגמה הבאה מסירה את מדיניות שמירת הנתונים מקטגוריה:


using