How to generate S3 presigned URL using boto3 and Python

If you want to give your users temporary access to a private S3 file without giving them access to the AWS console, you will need to generate an S3 presigned URL of your target file.

To generate and test the S3 presigned URL, you can try the code below.

Continue reading How to generate S3 presigned URL using boto3 and Python

How to download files from S3 Bucket using boto3 and Python

If you want to download a file from an AWS S3 Bucket using Python, then you can use the sample codes below.

The codes below use AWS SDK for Python named boto3.

boto3 provides three methods to download a file.

  1. download_file()
  2. download_fileobj() – with multipart upload
  3. get_object()

Then for each method, you can use the client class or the resource class of boto3.

Both of the classes will be used for each of the methods above.

Note: All examples will work with any Python3 environment running in Windows, MacOS or Linux operating systems.

Continue reading How to download files from S3 Bucket using boto3 and Python

How to read a JSON file in S3 and store it in a Dictionary using boto3 and Python

If you want to get a JSON file from an S3 Bucket and load it into a Python Dictionary then you can use the example codes below.

There are 4 scenarios for the examples scripts below.

  1. Basic JSON file from S3 to Python Dictionary
  2. With Try/Except block
  3. With datetime, date, and time conversions
  4. Running the code in a Lambda Function

AWS boto3 provides 2 ways to access S3 files, the boto3.client('s3') and boto3.resource('s3'). For each of the example scenarios above, a code will be provided for the two methods.

Related: Writing a Dictionary to JSON file in S3 using boto3 and Python

Since both methods will function the same, you can choose whichever method you like.

Continue reading How to read a JSON file in S3 and store it in a Dictionary using boto3 and Python

How to set the default screen resolution for VNC Viewer when Raspberry Pi is not connected to a monitor

A headless setup is when your Raspberry Pi is turned on without a monitor.

The display is not needed usually, especially if your access is only via SSH. The issue would be if you connect via VNC Viewer, the display will look like this.

The icons are too big but the screen is too small.

What’s happening here is that Raspberry Pi will use a very small screen resolution when it is not connected to a monitor. So when you connect via VNC Viewer, VNC will get the same small screen resolution.

To solve this, we need to change the default headless screen resolution of the Raspberry Pi. You can follow the instructions below to set this.

Changing the default screen resolution of a headless Raspberry Pi

Continue reading How to set the default screen resolution for VNC Viewer when Raspberry Pi is not connected to a monitor

How to connect to WiFi using nmcli

Below are commands that you can use to connect and check the status of your WiFi connection using nmcli.

I usually use this when connecting my Rock Pi running Ubuntu to the WiFi Network. But commands below will also work as long as you have the nmcli in your Linux System.

Commands

Turn On WiFi

# Turn on WiFi
nmcli r wifi on

Scan WiFi Networks

# Scan WiFi Networks
nmcli dev wifi
Continue reading How to connect to WiFi using nmcli

How to read a file in S3 and store it in a String using Python and boto3

If you want to get a file from an S3 Bucket and then put it in a Python string, try the examples below.

boto3, the AWS SDK for Python, offers two distinct methods for accessing files or objects in Amazon S3: client method and the resource method.

Option 1 uses the boto3.client('s3') method, while options 2 and 3 use the boto3.resource('s3') method.

All 3 options do the exact same thing so get the one that you feel comfortable with or the one that will fit your use case.


Continue reading How to read a file in S3 and store it in a String using Python and boto3

How to open Folder in Visual Studio Code using Ubuntu

There are two ways you can do to open a folder in Ubuntu and open it in Visual Studio Code.

Prerequisites

The steps below assume that you have installed the following prerequisites.

  • Ubuntu operating system. I tested using Ubuntu 22.04 but this will work in most latest Ubuntu Versions.
  • Visual Studio Code is already installed.
Continue reading How to open Folder in Visual Studio Code using Ubuntu

How to install PostgreSQL 15 on Ubuntu

If you try to run the command sudo apt install postgresql in Ubuntu it will install Postgresql version 14 instead of version 15. As of writing the Ubuntu repository does not include version 15.

Follow the tutorial below for a detailed guide to successfully installing PostgreSQL 15.


Step-by-Step Instructions on Configuring Postgresql 15 in Ubuntu

The following steps were tested in Ubuntu 22.04 LTS. If there are new versions of Ubuntu, I will test the steps again and update this post.

1. Installation

1.1 Add the Postgresql Package Repository to Ubuntu

Run the commands below to add the official Postgresql package repository to Ubuntu.

Continue reading How to install PostgreSQL 15 on Ubuntu

How to install a specific version of PostgreSQL in Ubuntu

If you run the command below in Ubuntu it will install the latest version of PostgreSQL in the Ubuntu repository.

# Installs the latest version of PostgreSQL
sudo apt install postgresql postgresql-client

As of writing it will install PostgreSQL version 14.


Continue reading How to install a specific version of PostgreSQL in Ubuntu

How to write a Dictionary to JSON file in S3 Bucket using boto3 and Python

If you want to write a python dictionary to a JSON file in S3 then you can use the code examples below.

There are two code examples doing the same thing below because boto3 provides a client method and a resource method to edit and access AWS S3.

Related: Reading a JSON file in S3 and store it in a Dictionary using boto3 and Python

Writing Python Dictionary to an S3 Object using boto3 Client

import boto3
import json
from datetime import date

data_dict = {
    'Name': 'Daikon Retek',
    'Birthdate': date(2000, 4, 7),
    'Subjects': ['Math', 'Science', 'History']
}

# Convert Dictionary to JSON String
data_string = json.dumps(data_dict, indent=2, default=str)


# Upload JSON String to an S3 Object
client = boto3.client('s3')

client.put_object(
    Bucket='radishlogic-bucket', 
    Key='s3_folder/client_data.json',
    Body=data_string
)
Continue reading How to write a Dictionary to JSON file in S3 Bucket using boto3 and Python