|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# snippet-start:[python.example_code.s3.S3ConditionalRequests.wrapper] |
| 5 | + |
| 6 | +import boto3 |
| 7 | +import logging |
| 8 | + |
| 9 | +from botocore.exceptions import ClientError |
| 10 | + |
| 11 | +# Configure logging |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +# snippet-start:[python.example_code.s3.helper.S3ConditionalRequests] |
| 16 | +class S3ConditionalRequests: |
| 17 | + """Encapsulates S3 conditional request operations.""" |
| 18 | + |
| 19 | + def __init__(self, s3_client): |
| 20 | + self.s3 = s3_client |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def from_client(cls): |
| 24 | + """ |
| 25 | + Instantiates this class from a Boto3 client. |
| 26 | + """ |
| 27 | + s3_client = boto3.client("s3") |
| 28 | + return cls(s3_client) |
| 29 | + |
| 30 | + # snippet-end:[python.example_code.s3.helper.S3ConditionalRequests] |
| 31 | + |
| 32 | + # snippet-start:[python.example_code.s3.GetObjectConditional] |
| 33 | + |
| 34 | + def get_object_conditional( |
| 35 | + self, |
| 36 | + object_key: str, |
| 37 | + source_bucket: str, |
| 38 | + condition_type: str, |
| 39 | + condition_value: str, |
| 40 | + ): |
| 41 | + """ |
| 42 | + Retrieves an object from Amazon S3 with a conditional request. |
| 43 | +
|
| 44 | + :param object_key: The key of the object to retrieve. |
| 45 | + :param source_bucket: The source bucket of the object. |
| 46 | + :param condition_type: The type of condition: 'IfMatch', 'IfNoneMatch', 'IfModifiedSince', 'IfUnmodifiedSince'. |
| 47 | + :param condition_value: The value to use for the condition. |
| 48 | + """ |
| 49 | + try: |
| 50 | + response = self.s3.get_object( |
| 51 | + Bucket=source_bucket, |
| 52 | + Key=object_key, |
| 53 | + **{condition_type: condition_value}, |
| 54 | + ) |
| 55 | + sample_bytes = response["Body"].read(20) |
| 56 | + print( |
| 57 | + f"\tConditional read successful. Here are the first 20 bytes of the object:\n" |
| 58 | + ) |
| 59 | + print(f"\t{sample_bytes}") |
| 60 | + except ClientError as e: |
| 61 | + error_code = e.response["Error"]["Code"] |
| 62 | + if error_code == "PreconditionFailed": |
| 63 | + print("\tConditional read failed: Precondition failed") |
| 64 | + elif error_code == "304": # Not modified error code. |
| 65 | + print("\tConditional read failed: Object not modified") |
| 66 | + else: |
| 67 | + logger.error(f"Unexpected error: {error_code}") |
| 68 | + raise |
| 69 | + |
| 70 | + # snippet-end:[python.example_code.s3.GetObjectConditional] |
| 71 | + |
| 72 | + # snippet-start:[python.example_code.s3.PutObjectConditional] |
| 73 | + |
| 74 | + def put_object_conditional(self, object_key: str, source_bucket: str, data: bytes): |
| 75 | + """ |
| 76 | + Uploads an object to Amazon S3 with a conditional request. Prevents overwrite |
| 77 | + using an IfNoneMatch condition for the object key. |
| 78 | +
|
| 79 | + :param object_key: The key of the object to upload. |
| 80 | + :param source_bucket: The source bucket of the object. |
| 81 | + :param data: The data to upload. |
| 82 | + """ |
| 83 | + try: |
| 84 | + self.s3.put_object( |
| 85 | + Bucket=source_bucket, Key=object_key, Body=data, IfNoneMatch="*" |
| 86 | + ) |
| 87 | + print( |
| 88 | + f"\tConditional write successful for key {object_key} in bucket {source_bucket}." |
| 89 | + ) |
| 90 | + except ClientError as e: |
| 91 | + error_code = e.response["Error"]["Code"] |
| 92 | + if error_code == "PreconditionFailed": |
| 93 | + print("\tConditional write failed: Precondition failed") |
| 94 | + else: |
| 95 | + logger.error(f"Unexpected error: {error_code}") |
| 96 | + raise |
| 97 | + |
| 98 | + # snippet-end:[python.example_code.s3.PutObjectConditional] |
| 99 | + |
| 100 | + # snippet-start:[python.example_code.s3.CopyObjectConditional] |
| 101 | + def copy_object_conditional( |
| 102 | + self, |
| 103 | + source_key: str, |
| 104 | + dest_key: str, |
| 105 | + source_bucket: str, |
| 106 | + dest_bucket: str, |
| 107 | + condition_type: str, |
| 108 | + condition_value: str, |
| 109 | + ): |
| 110 | + """ |
| 111 | + Copies an object from one Amazon S3 bucket to another with a conditional request. |
| 112 | +
|
| 113 | + :param source_key: The key of the source object to copy. |
| 114 | + :param dest_key: The key of the destination object. |
| 115 | + :param source_bucket: The source bucket of the object. |
| 116 | + :param dest_bucket: The destination bucket of the object. |
| 117 | + :param condition_type: The type of condition to apply, e.g. |
| 118 | + 'CopySourceIfMatch', 'CopySourceIfNoneMatch', 'CopySourceIfModifiedSince', 'CopySourceIfUnmodifiedSince'. |
| 119 | + :param condition_value: The value to use for the condition. |
| 120 | + """ |
| 121 | + try: |
| 122 | + self.s3.copy_object( |
| 123 | + Bucket=dest_bucket, |
| 124 | + Key=dest_key, |
| 125 | + CopySource={"Bucket": source_bucket, "Key": source_key}, |
| 126 | + **{condition_type: condition_value}, |
| 127 | + ) |
| 128 | + print( |
| 129 | + f"\tConditional copy successful for key {dest_key} in bucket {dest_bucket}." |
| 130 | + ) |
| 131 | + except ClientError as e: |
| 132 | + error_code = e.response["Error"]["Code"] |
| 133 | + if error_code == "PreconditionFailed": |
| 134 | + print("\tConditional copy failed: Precondition failed") |
| 135 | + elif error_code == "304": # Not modified error code. |
| 136 | + print("\tConditional copy failed: Object not modified") |
| 137 | + else: |
| 138 | + logger.error(f"Unexpected error: {error_code}") |
| 139 | + raise |
| 140 | + |
| 141 | + # snippet-end:[python.example_code.s3.CopyObjectConditional] |
| 142 | +# snippet-end:[python.example_code.s3.S3ConditionalRequests.wrapper] |
0 commit comments