Download XLSX files from s3 Bucket using apigatewayproxies / lambda

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

New contributor

Kayla Pineda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật