import boto3
from botocore.exceptions import ClientError
import time

# A script to create an Amazon S3 Glacier vault using Boto3.

# --- Configuration ---
REGION = "us-east-1"
VAULT_NAME = "MyBoto3GlacierVault"

glacier_client = boto3.client('glacier', region_name=REGION)

def create_glacier_vault():
    """Creates a Glacier vault."""
    print(f"--- Creating Glacier Vault: {VAULT_NAME} ---")
    try:
        glacier_client.create_vault(vaultName=VAULT_NAME)
        print(f"Glacier Vault '{VAULT_NAME}' created.")
    except ClientError as e:
        if e.response['Error']['Code'] == 'ResourceAlreadyExistsException':
            print(f"Glacier Vault '{VAULT_NAME}' already exists. Skipping creation.")
        else:
            print(f"Error creating vault: {e}")
            raise

def delete_glacier_vault():
    """Deletes the Glacier vault."""
    print(f"\n--- Deleting Glacier Vault: {VAULT_NAME} ---")
    try:
        glacier_client.delete_vault(vaultName=VAULT_NAME)
        print("Glacier Vault deleted.")
    except ClientError as e:
        if e.response['Error']['Code'] == 'ResourceNotFoundException':
            print(f"Glacier Vault '{VAULT_NAME}' not found, skipping deletion.")
        elif e.response['Error']['Code'] == 'InvalidParameterValueException' and 'vault is not empty' in str(e):
            print(f"Glacier Vault '{VAULT_NAME}' is not empty. Please delete all archives before deleting the vault.")
        else:
            print(f"Error deleting vault: {e}")
            raise

def main():
    try:
        create_glacier_vault()

        print("\n--- Glacier Vault Setup Complete! ---")
        print(f"Vault Name: {VAULT_NAME}")
        print("You can now upload archives to this vault.")

        input("Press Enter to delete the Glacier Vault...")

    except ClientError as e:
        print(f"An AWS client error occurred: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
    finally:
        delete_glacier_vault()
        print("\n--- Glacier vault demonstration and cleanup complete ---")

if __name__ == "__main__":
    main()
