Home > database >  Using boto3 for GCP retention settings
Using boto3 for GCP retention settings

Time:03-08

I am using the python boto3 package to access AWS S3 and GCP buckets. These buckets have object lock/retention set to their respective buckets. Is there a common packge/SDK that would fetch the lock/retention details.

        session = boto3.session.Session(aws_access_key_id=access_key,
                                        aws_secret_access_key=secret_key,
                                        region_name=region_name)
        s3_client = session.client('s3', endpoint_url=endpoint)
        head = s3_client.head_bucket(Bucket=bucket_name)
        result = s3_client.get_object_lock_configuration(Bucket=bucket_name)

This code block works for AWS S3 but throws exception for GCP buckets. Exception thrown:

[DEBUG] Response body: b"<?xml version='1.0' encoding='UTF-8'?><Error><Code>InvalidArgument</Code>
<Message>Invalid argument.</Message><Details>Invalid query parameter(s): [object-lock]</Details></Error>" 2022-03-03 20:16:37,111 botocore.hooks 
[DEBUG] Event needs-retry.s3.GetObjectLockConfiguration: calling handler <botocore.retryhandler.RetryHandler object at 0x7f518264a310> 2022-03-03 20:16:37,112 botocore.retryhandler 
[DEBUG] No retry needed. 2022-03-03 20:16:37,113 botocore.hooks 
[DEBUG] Event needs-retry.s3.GetObjectLockConfiguration: calling handler <bound method S3RegionRedirector.redirect_from_error of <botocore.utils.S3RegionRedirector object at 0x7f518264a370>> 
An error occurred (InvalidArgument) when calling the GetObjectLockConfiguration operation: Invalid argument.

Thank you!!

CodePudding user response:

Google Cloud storage has a separate set of commands within the SDK - gsutil.
You can use retention command to obtain the details.

There is also an API that allows fetching multiple information from buckets and objects.
Send retentionPolicy, retentionPeriod or effectiveTime in fields parameter to filter only the values you are interested in.

  • Related