Skip to content

Instantly share code, notes, and snippets.

@epjuan21
Last active September 24, 2021 08:29
Show Gist options
  • Select an option

  • Save epjuan21/93bf17713a31d82ed394a1c71af8ee8b to your computer and use it in GitHub Desktop.

Select an option

Save epjuan21/93bf17713a31d82ed394a1c71af8ee8b to your computer and use it in GitHub Desktop.
Subir archivo a Amazon Glacier con Python y Boto3
import logging
import boto3
from botocore.exceptions import ClientError
def upload_archive(vault_name, src_data):
"""Agrega un archivo a una bodega de Amazon Glacier .
La carga se produce de forma sincrónica.
:param vault_name: string
:param src_data: bytes de datos o referencia de cadena a la especificación del archivo
:Si se agregó src_data a la bóveda, devuelva el diccionario de la información del archivo; de lo contrario, Ninguno
"""
# El argumento src_data debe ser de tipo bytes o cadena
# Constructor body= parameter
if isinstance(src_data, bytes):
object_data = src_data
elif isinstance(src_data, str):
try:
object_data = open(src_data, 'rb')
# possible FileNotFoundError/IOError exception
except Exception as e:
logging.error(e)
return None
else:
logging.error('Type of ' + str(type(src_data)) +
' for the argument \'src_data\' is not supported.')
return None
glacier = boto3.client('glacier')
try:
archive = glacier.upload_archive(vaultName=vault_name,
body=object_data)
except ClientError as e:
logging.error(e)
return None
finally:
if isinstance(src_data, str):
object_data.close()
# Return dictionary of archive information
return archive
def main():
# Assign these values before running the program
test_vault_name = 'XXXX VAULT NAME'
filename = "XXX PATH\\TO\\FILE.extension"
archive = upload_archive(test_vault_name, filename)
if archive is not None:
logging.info(f'Archive {archive["archiveId"]} added to {test_vault_name}')
print(f'Archive {archive["archiveId"]} added to {test_vault_name}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment