Ramesh
Ramesh

Reputation: 775

boto3 dict to json to text format in Python

I'm trying to create a text file from my boto3 response in a AWS Lambda function which I want to upload to my S3. Here's what I have so far.

          import boto3
          import json
          s3 = boto3.client('s3')
          clientVPN = boto3.client('ec2')
          clientVPN = clientVPN.export_client_vpn_client_configuration(ClientVpnEndpointId='arn-of-clientVPN-id', DryRun=False)
              clientVPN_string = json.dumps(clientVPN, indent=2)
              response = s3.put_object(Bucket=myBucket, Body=clientVPN_string, Key='configuration.ovpn')

With what I have above, I'm able to write the file into S3. However, the file looks like below:

{
  "ClientConfiguration": "client\ndev tun\nproto udp\nremote xxxx.us-east-1.amazonaws.com 443\nremote-random-hostname\nresolv-retry infinite\nnobind\nremote-cert-tls server\ncipher AES-256-GCM\nverb 3\n<ca>\n-----BEGIN CERTIFICATE-----\nxx/2x\ncxxBr\nkw==\n-----END CERTIFICATE-----\n\n</ca>\n\n\nreneg-sec 0",
  "ResponseMetadata": {
    "RequestId": "40b87d13-edb9-4f3e-85e0-9b9f233518d9",
    "HTTPStatusCode": 200,
    "HTTPHeaders": {
      "x-amzn-requestid": "40b87d13-edb9-4f3e-85e0-9b9f233518d9",
      "cache-control": "no-cache, no-store",
      "strict-transport-security": "max-age=31536000; includeSubDomains",
      "vary": "accept-encoding",
      "content-type": "text/xml;charset=UTF-8",
      "transfer-encoding": "chunked",
      "date": "Sat, 26 Feb 2022 19:25:22 GMT",
      "server": "AmazonEC2"
    },
    "RetryAttempts": 0
  }
}

However, the output from AWS CLI gives me a neat txt format which would look as below:

client
dev tun
proto udp
remote xxxx.amazonaws.com 443
remote-random-hostname
resolv-retry infinite
nobind
remote-cert-tls server
cipher AES-256-GCM
verb 3
<ca>
-----BEGIN CERTIFICATE-----
xx
-----END CERTIFICATE-----

</ca>


reneg-sec 0

As we can see in the output, I would like to extract only the "ClientConfiguration" and neatly formatted as lines as provided by the AWS CLI output. I've tried several ways to do it like below but unfortunately I'm not having luck.

   clientVPN_string = json.dumps(clientVPN, indent=2)
   for key, value in clientVPN_string.items():
     with open('client.ovpn', 'a') as the_file:
       print(value)

Is there any way to convert the output am getting with boto3 to the txt format like how AWS CLI provides? Thank you!

Upvotes: 0

Views: 717

Answers (1)

valdeci
valdeci

Reputation: 15237

Using the methods decode("utf-8") and json.loads() will work for you.

Example:

def get_data_from_s3(name):
    s3 = boto3.client('s3')
    bucket = 'bucket_name'
    result = s3.list_objects(Bucket=bucket)
    for o in result.get('Contents'):
        data = s3.get_object(Bucket=bucket, Key=o.get('Key'))
        contents = data['Body'].read()
        client_vpn_body = contents.decode("utf-8")
        client_vpn_json = json.loads(client_vpn_body)
        print(client_vpn_json['ClientConfiguration'])

Upvotes: 1

Related Questions