I am trying to fix the problem where downloading xlsx files from s3 bucket, the set up UI using angular and then then backend using python, the front end is all good but the backend is the issue, downloading csv files is not an issue but when downloading xlsx files is where the issue lies, currently using apigateway i am using apiGatewayServiceProxies: as a custom in the yaml file to download and get the objects (xlsx files) whenever I do this the file returns corrupted and double the size, i tried to make a lambda to download instead of using the apiGatewayServiceProxies, i was wondering if there was a way to keep using this?
I tried to use a lambda instead but i ended up with the issue of having to use pandas other external libraries which I rather not use, is there any other solutions?
I've tried
apiGatewayServiceProxies:
- s3:
path: /s3
method: get
action: GetObject
contextHandling: CONVERT_TO_BINARY
and
apiGateway:
binaryMediaTypes:
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
I got the error “child “s3” fails because [“contentHandling” is not allowed]”
Trying and creating a new lambda I did:
s3_client = boto3.client('s3')
def handle(event, context):
logger.debug(f"Received event: {event}")
logger.debug(f"Context: {context}")
bucket = os.environ['S3_BUCKET']
add_keys_to_log_context(bucket=bucket)
key = extract_key_from_event(event)
add_keys_to_log_context(key=key)
if not key:
error_message = "Missing 'key' query parameter"
logger.error(error_message)
raise SkyPilException(HTTPStatus.BAD_REQUEST, error_message)
try:
file_content, content_type = get_file_from_s3(bucket, key)
return {
'statusCode': HTTPStatus.OK,
'headers': {
'Content-Type': content_type,
'Content-Disposition': f'attachment; filename={os.path.basename(key)}'
},
'body': file_content, # Send the raw binary data
'isBase64Encoded': False # Set this to False since it's raw binary data
}
except Exception as e:
msg, title = extract_error_msg(e)
logger.error(f"Error: {msg}, Title: {title}")
return {
"statusCode": HTTPStatus.INTERNAL_SERVER_ERROR,
"body": f"Error fetching file: {msg}",
"headers": {
'Content-Type': 'text/plain'
}
}
def extract_key_from_event(event: dict) -> str:
key = event.get("queryStringParameters", {}).get('key') or event.get("query", {}).get('key')
return key
def extract_error_msg(e: Exception) -> tuple:
msg = str(e)
try:
error_msg, error_title = msg.split(":", 1)
except ValueError:
error_msg = msg
error_title = None
return error_msg.strip(), error_title
def get_file_from_s3(bucket_name: str, key: str) -> tuple:
logger.debug(f"Fetching file from S3 - Bucket: {bucket_name}, Key: {key}")
try:
file_stream = io.BytesIO()
s3_client.download_fileobj(bucket_name, key, file_stream)
file_stream.seek(0)
file_content = file_stream.read() # Read as binary
content_type = s3_client.head_object(Bucket=bucket_name, Key=key)['ContentType']
logger.debug(f"Fetched file content of length {len(file_content)} and content type {content_type}")
return file_content, content_type
except s3_client.exceptions.NoSuchKey:
error_message = f"File not found in S3 bucket: {bucket_name}, key: {key}"
logger.error(error_message)
raise SkyPilException(HTTPStatus.NOT_FOUND, error_message)
except s3_client.exceptions.ClientError as e:
logger.error(f"ClientError fetching file from S3: {e}")
raise
except Exception as e:
logger.error(f"Error fetching file from S3: {e}")
raise
Kayla Pineda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.