UPD: manually optimizaed code written by cursor

This commit is contained in:
xpk
2025-11-11 16:23:23 +08:00
parent b74c82943b
commit 352d06e317
+14 -28
View File
@@ -12,18 +12,15 @@ aws s3 ls s3://$BUCKET/$PREFIX | awk "{print \"$BUCKET,$PREFIX\"\$NF}" | tee /tm
import sys import sys
import json import json
import secrets
import string
import time import time
import boto3 import boto3
import random
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
def generate_random_id(length=4): def generate_random_id():
"""Generate a random alphanumeric ID of specified length.""" """Generate a random alphanumeric ID of specified length."""
# Use lowercase alphanumeric characters (0-9, a-z) return random.randint(1000, 9999)
characters = string.digits
return ''.join(secrets.choice(characters) for _ in range(length))
def create_trust_policy(): def create_trust_policy():
@@ -76,23 +73,13 @@ def create_iam_role(iam_client, role_name="S3BatchRestoreRole"):
return role_name return role_name
def create_manifest_bucket(s3_client, bucket_name, region="ap-east-1"): def create_manifest_bucket(s3_client, bucket_name, region):
"""Create S3 bucket for manifest file in the specified region.""" """Create S3 bucket for manifest file."""
try:
# For regions other than us-east-1, specify LocationConstraint
if region == "us-east-1":
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client.create_bucket( s3_client.create_bucket(
Bucket=bucket_name, Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region} CreateBucketConfiguration={'LocationConstraint': region}
) )
print(f"Created manifest bucket: {bucket_name} in region: {region}") print(f"Created manifest bucket: {bucket_name}")
except ClientError as e:
if e.response['Error']['Code'] == 'BucketAlreadyExists':
print(f"Bucket {bucket_name} already exists")
else:
raise
def upload_manifest(s3_client, bucket_name, manifest_file_path, object_key="objectlist.csv"): def upload_manifest(s3_client, bucket_name, manifest_file_path, object_key="objectlist.csv"):
@@ -204,13 +191,12 @@ def main():
manifest_file = sys.argv[1] manifest_file = sys.argv[1]
# Initialize AWS clients # Initialize AWS clients
# Note: IAM and STS are global services, but S3 and S3 Control are region-specific region = "ap-east-1"
region = "ap-east-1" # Specify the region for S3 operations session = boto3.Session(region_name=region)
session = boto3.Session() iam_client = session.client('iam')
iam_client = session.client('iam') # IAM is global s3_client = session.client('s3')
s3_client = session.client('s3', region_name=region) # S3 client for ap-east-1 s3control_client = session.client('s3control')
s3control_client = session.client('s3control', region_name=region) # S3 Control for ap-east-1 sts_client = session.client('sts')
sts_client = session.client('sts') # STS is global
# Get account ID # Get account ID
account_id = get_account_id(sts_client) account_id = get_account_id(sts_client)
@@ -221,9 +207,9 @@ def main():
role_arn = f"arn:aws:iam::{account_id}:role/{role_name}" role_arn = f"arn:aws:iam::{account_id}:role/{role_name}"
# Create manifest bucket # Create manifest bucket
random_id = generate_random_id(4) random_id = generate_random_id()
manifest_bucket = f"deep-archive-batch-restore-{random_id}" manifest_bucket = f"deep-archive-batch-restore-{random_id}"
create_manifest_bucket(s3_client, manifest_bucket, region=region) create_manifest_bucket(s3_client, manifest_bucket, session.region_name)
# Upload manifest and get ETag # Upload manifest and get ETag
etag = upload_manifest(s3_client, manifest_bucket, manifest_file) etag = upload_manifest(s3_client, manifest_bucket, manifest_file)