While creating a GCS bucket using python code I am getting below error
cmd = "gcloud auth application-default login >$null 2>&1"
returned_value = subprocess.call(cmd, shell=True)
# Initialize the client
storage_client = storage.Client(project="03114")
# Bucket name and region
bucket_name = "automationbuckettesting-name"
region_name = "us-west1"
bucket = storage_client.create_bucket(bucket_name,location=region_name)
Error:
‘604800’ violates constraint ‘constraints/storage.softDeletePolicySeconds’
I am looking for the Python code to create a bucket
Kunal Sapkal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I’ve not encountered this previously.
Your organization has a soft delete retention policy.
604800 (seconds) is 7 days.
Per the documentation, “Once set, the bucket soft delete policy must include one of the specified durations.”
I think the solution is to create the Bucket differently:
import os
from google.cloud import storage
PROJECT = os.getenv("PROJECT")
LOCATION = os.getenv("LOCATION")
BUCKET = os.getenv("BUCKET")
storage_client = storage.Client(project=PROJECT)
bucket = storage_client.bucket(BUCKET)
bucket.soft_delete_policy.retention_duration_seconds = 604800
bucket.create(location=LOCATION)
Yields:
gcloud storage buckets list --project=${PROJECT}
--format="yaml(name,soft_delete_policy.retentionDurationSeconds)"
name: {bucket}
soft_delete_policy:
retentionDurationSeconds: '604800'
Your preamble is an anti-pattern. Applications Default Credentials are designed to (and should be) excluded from you code. If your code were deployed to e.g. a Google Cloud compute service, your code as-is would fail. Removing these lines will improve the portability of your code:
cmd = "gcloud auth application-default login >$null 2>&1"
returned_value = subprocess.call(cmd, shell=True)
2