UPD: manually optimizaed code written by cursor
This commit is contained in:
+18
-32
@@ -12,18 +12,15 @@ aws s3 ls s3://$BUCKET/$PREFIX | awk "{print \"$BUCKET,$PREFIX\"\$NF}" | tee /tm
|
||||
|
||||
import sys
|
||||
import json
|
||||
import secrets
|
||||
import string
|
||||
import time
|
||||
import boto3
|
||||
import random
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
|
||||
def generate_random_id(length=4):
|
||||
def generate_random_id():
|
||||
"""Generate a random alphanumeric ID of specified length."""
|
||||
# Use lowercase alphanumeric characters (0-9, a-z)
|
||||
characters = string.digits
|
||||
return ''.join(secrets.choice(characters) for _ in range(length))
|
||||
return random.randint(1000, 9999)
|
||||
|
||||
|
||||
def create_trust_policy():
|
||||
@@ -76,23 +73,13 @@ def create_iam_role(iam_client, role_name="S3BatchRestoreRole"):
|
||||
return role_name
|
||||
|
||||
|
||||
def create_manifest_bucket(s3_client, bucket_name, region="ap-east-1"):
|
||||
"""Create S3 bucket for manifest file in the specified region."""
|
||||
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(
|
||||
Bucket=bucket_name,
|
||||
CreateBucketConfiguration={'LocationConstraint': region}
|
||||
)
|
||||
print(f"Created manifest bucket: {bucket_name} in region: {region}")
|
||||
except ClientError as e:
|
||||
if e.response['Error']['Code'] == 'BucketAlreadyExists':
|
||||
print(f"Bucket {bucket_name} already exists")
|
||||
else:
|
||||
raise
|
||||
def create_manifest_bucket(s3_client, bucket_name, region):
|
||||
"""Create S3 bucket for manifest file."""
|
||||
s3_client.create_bucket(
|
||||
Bucket=bucket_name,
|
||||
CreateBucketConfiguration={'LocationConstraint': region}
|
||||
)
|
||||
print(f"Created manifest bucket: {bucket_name}")
|
||||
|
||||
|
||||
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]
|
||||
|
||||
# Initialize AWS clients
|
||||
# Note: IAM and STS are global services, but S3 and S3 Control are region-specific
|
||||
region = "ap-east-1" # Specify the region for S3 operations
|
||||
session = boto3.Session()
|
||||
iam_client = session.client('iam') # IAM is global
|
||||
s3_client = session.client('s3', region_name=region) # S3 client for ap-east-1
|
||||
s3control_client = session.client('s3control', region_name=region) # S3 Control for ap-east-1
|
||||
sts_client = session.client('sts') # STS is global
|
||||
region = "ap-east-1"
|
||||
session = boto3.Session(region_name=region)
|
||||
iam_client = session.client('iam')
|
||||
s3_client = session.client('s3')
|
||||
s3control_client = session.client('s3control')
|
||||
sts_client = session.client('sts')
|
||||
|
||||
# Get account ID
|
||||
account_id = get_account_id(sts_client)
|
||||
@@ -221,9 +207,9 @@ def main():
|
||||
role_arn = f"arn:aws:iam::{account_id}:role/{role_name}"
|
||||
|
||||
# Create manifest bucket
|
||||
random_id = generate_random_id(4)
|
||||
random_id = generate_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
|
||||
etag = upload_manifest(s3_client, manifest_bucket, manifest_file)
|
||||
|
||||
Reference in New Issue
Block a user