2 Answers
- Newest
- Most votes
- Most comments
0
try something like this.. pls check you key values accordingly
#!/bin/bash
# Set your AWS CLI profile, bucket name, and object key
AWS_PROFILE="your_aws_profile"
BUCKET_NAME="your_bucket_name"
OBJECT_KEY="your_object_key"
# Get the current object tagging
TAG_INFO=$(aws s3api get-object-tagging --bucket $BUCKET_NAME --key $OBJECT_KEY --profile $AWS_PROFILE --output json)
# Check if the desired retention tag is already applied
if [[ $TAG_INFO == *"\"Key\":\"retention\",\"Value\":\"10yearsretention\""* ]] || [[ $TAG_INFO == *"\"Key\":\"retention\",\"Value\":\"6yearsretention\""* ]]; then
# Retention tag is already applied
echo "Retention tag already applied to the S3 object. Skipping."
else
# Retention tag not applied, apply the 10-year retention tag
echo "Applying 10-year retention tag to the S3 object..."
aws s3api put-object-tagging --bucket $BUCKET_NAME --key $OBJECT_KEY --tagging 'TagSet=[{Key=retention,Value=10yearsretention}]' --profile $AWS_PROFILE
echo "Retention tag applied."
fi
answered a year ago
0
Thanks for your example @Tulasiram toorpu, I tried the above example, please check my observations below
I did like below: Manually i applied the retention tag to one S3 object and in the same folder other 3 objects are not having retention tag. So total 4 object only 1 is applied.
I ran the script for the first object it shows log like "Retention tag is already applied to specified S3 object. Skipping" Script completed
Second run, it is appling the retention tag to the already applied S3 object and not checking if that tag is already having key and value or not.
Third run, same thing happened likewise second run.
?
answered a year ago
Relevant content
- asked 3 months ago
- asked 5 months ago
- Accepted Answerasked 7 years ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 2 years ago