Encapsulates Amazon S3 Glacier API operations.
| 22 | |
| 23 | # snippet-start:[python.example_code.glacier.GlacierWrapper] |
| 24 | class GlacierWrapper: |
| 25 | """Encapsulates Amazon S3 Glacier API operations.""" |
| 26 | |
| 27 | def __init__(self, glacier_resource): |
| 28 | """ |
| 29 | :param glacier_resource: A Boto3 Amazon S3 Glacier resource. |
| 30 | """ |
| 31 | self.glacier_resource = glacier_resource |
| 32 | |
| 33 | # snippet-end:[python.example_code.glacier.GlacierWrapper] |
| 34 | |
| 35 | # snippet-start:[python.example_code.glacier.CreateVault] |
| 36 | def create_vault(self, vault_name): |
| 37 | """ |
| 38 | Creates a vault. |
| 39 | |
| 40 | :param vault_name: The name to give the vault. |
| 41 | :return: The newly created vault. |
| 42 | """ |
| 43 | try: |
| 44 | vault = self.glacier_resource.create_vault(vaultName=vault_name) |
| 45 | logger.info("Created vault %s.", vault_name) |
| 46 | except ClientError: |
| 47 | logger.exception("Couldn't create vault %s.", vault_name) |
| 48 | raise |
| 49 | else: |
| 50 | return vault |
| 51 | |
| 52 | # snippet-end:[python.example_code.glacier.CreateVault] |
| 53 | |
| 54 | # snippet-start:[python.example_code.glacier.ListVaults] |
| 55 | def list_vaults(self): |
| 56 | """ |
| 57 | Lists vaults for the current account. |
| 58 | """ |
| 59 | try: |
| 60 | for vault in self.glacier_resource.vaults.all(): |
| 61 | logger.info("Got vault %s.", vault.name) |
| 62 | except ClientError: |
| 63 | logger.exception("Couldn't list vaults.") |
| 64 | raise |
| 65 | |
| 66 | # snippet-end:[python.example_code.glacier.ListVaults] |
| 67 | |
| 68 | # snippet-start:[python.example_code.glacier.UploadArchive] |
| 69 | @staticmethod |
| 70 | def upload_archive(vault, archive_description, archive_file): |
| 71 | """ |
| 72 | Uploads an archive to a vault. |
| 73 | |
| 74 | :param vault: The vault where the archive is put. |
| 75 | :param archive_description: A description of the archive. |
| 76 | :param archive_file: The archive file to put in the vault. |
| 77 | :return: The uploaded archive. |
| 78 | """ |
| 79 | try: |
| 80 | archive = vault.upload_archive( |
| 81 | archiveDescription=archive_description, body=archive_file |
no outgoing calls