View all our articles

Save your PDF to your Amazon S3 Bucket in Python with aiohttp

In this guide, we'll show you how you can generate a PDF document from HTML with PDFShift's API and save it to your own Amazon S3 bucket. This allows you to generate document and automate actions once the document has been created on your bucket.

First, you'll need to set up permissions in your bucket to allow PDFShift to write in it.

We crafted a very simple policy that only does what is required and nothing more. You can copy/paste it and apply it on your bucket, but don't forget to change the name of your bucket:

    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "Allows to write the generated PDF in this bucket.",
        "Effect": "Allow",
        "Principal": {
            "AWS": ["arn:aws:iam::804461045055:user/pdfshift"]
        "Action": ["s3:PutObject"],
        "Resource": "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"

Once this is done, you can send a request to PDFShift by adding the s3_destination parameter. It takes the full s3 bucket as a URL. Once the conversion will be done, PDFShift will

import aiohttp, asyncio, json, base64

# You can get an API key at
api_key = 'sk_xxxxxxxxxxxx'

params = {
    'source': '',
    's3_destination': 's3://DOC-EXAMPLE-BUCKET/test/example.pdf'

response = None
    auth = base64.b64encode(

    async with aiohttp.ClientSession() as session:
        async with
            headers={'Authorization': f'Basic {auth}'},
        ) as response:
            if response.status >= 400:
                raise Exception('Invalid request: {}'.format(await response.text()))

            print('The PDF document was generated and saved to your S3 Bucket')
except asyncio.TimeoutError:
    raise Exception('The request took too long to process')
except aiohttp.ClientError as e:
    raise Exception(f'An error occurred: {e}')
except Exception as e:
    # We highly recommend you to handle exceptions. Often, PDFShift will provide you with a clear explanation about what happened.
    # Moreover, in case of error, no PDF are returned !
    raise Exception(f'An error occurred: {e}')

Once the conversion is done, PDFShift will save the content to your given s3_destination path and will return a response with a JSON body, such as:

    "success": true,
    "url": "",
    "filesize": 34980,
    "duration": 1423,
    "response": {
        "status-code": 200,
        "content-length": 0,
        "requests": 0,
        "duration": 1263.254446029663
    "executed": "2024-03-06T10:13:33.154960",
    "pdf_pages": 1

In the event you haven't setup the permissions properly, PDFShift will fail with the following JSON body error:

    "success": false,
    "error": "The S3 destination you provided can not be accessed.",
    "code": 400

This features is really interesting as it allows you to not have to write the logic once the file was generated if your intention is to backup the file (such as an invoice, a report or saving a document).

You can couple it with the webhook parameter to increase the speed of processing. By doing so, PDFShift will treat your conversion request asynchronously and save the document in the given path. The request made to PDFShift will be immediate.

For further details on the s3_destination property and its usage, please refer to our dedicated documentation.

We hope this guide was helpful. If you have any questions or noticed any issues on the code above,
feel free to drop us a line.