<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Vivek Shukla - Tutorial</title>
    <subtitle>I am a Software Engineer from India. I am currently building Daestro. I work in Rust, Javascript and Python.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://vshukla.com/tags/tutorial/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://vshukla.com/tags/tutorial/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2023-10-20T00:00:00+00:00</updated>
    <id>https://vshukla.com/tags/tutorial/atom.xml</id>
    <entry xml:lang="en">
        <title>Automate Postgres Database Backup and Upload to S3</title>
        <published>2023-10-20T00:00:00+00:00</published>
        <updated>2023-10-20T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/automate-postgres-database-backup-and-upload/"/>
        <id>https://vshukla.com/tech/automate-postgres-database-backup-and-upload/</id>
        <summary type="html">In this tutorial we will create a bash script to automate postgres database backup from a docker container and native postgres installation then upload to S3 based storage system.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/automate-postgres-database-backup-and-upload/">&lt;p&gt;Database is a very critical system for an application. To prepare for any disaster we all must take regular backups of our database.&lt;&#x2F;p&gt;
&lt;p&gt;In this tutorial we will create a bash script to automate &lt;a href=&quot;&#x2F;tech&#x2F;postgres-database-backup-from-docker-container&quot;&gt;postgres database backup from a docker container&lt;&#x2F;a&gt; and native postgres installation then upload to S3 based storage system.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-we-will-cover&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-we-will-cover&quot; aria-label=&quot;Anchor link for: what-we-will-cover&quot;&gt;🔗&lt;&#x2F;a&gt;What we will cover&lt;&#x2F;h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Write db backup automation script for docker and non-docker based system&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Upload backup file to AWS S3&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Setup the cron job for daily backup&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;steps&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#steps&quot; aria-label=&quot;Anchor link for: steps&quot;&gt;🔗&lt;&#x2F;a&gt;Steps&lt;&#x2F;h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Move to your project folder, where docker-compose.yml is.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Take the database backup in container&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Copy the backup file from container filesystem to our desired location&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Delete the backup file inside the container&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Upload backup file to S3&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Delete the local backup file&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Code snippet below covers step 1 to 4. But it is not optimal, later we will make modification so that we can easily automate it and make it re-usable.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; project_dir&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec db_service_name sh&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;pg_dump -U db_user db_name &amp;gt; filename&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose cp db_service_name:filename destination&#x2F;filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec db_service_name rm filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;upload-to-s3&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#upload-to-s3&quot; aria-label=&quot;Anchor link for: upload-to-s3&quot;&gt;🔗&lt;&#x2F;a&gt;Upload to S3&lt;&#x2F;h2&gt;
&lt;p&gt;To upload file to S3, we need to have aws cli installed, you can follow &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;cli&#x2F;latest&#x2F;userguide&#x2F;getting-started-install.html&quot;&gt;this guide from AWS docs to install awscli&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After installing awscli, you need to run &lt;code&gt;aws configure&lt;&#x2F;code&gt; and provide your key id and key secret. You can use any S3 based storage system here.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;aws s3 cp filename s3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;your&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;bucket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are using some other S3 based storage system then you’ll have to provide custom &lt;code&gt;--endpoint-url&lt;&#x2F;code&gt; argument here, make sure you read the respective docs for this.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;full-script&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#full-script&quot; aria-label=&quot;Anchor link for: full-script&quot;&gt;🔗&lt;&#x2F;a&gt;Full Script&lt;&#x2F;h2&gt;
&lt;p&gt;We have laid out all the steps and code to accomplish that, now let’s modify in such a way that we can easily automate it and make it reusable.&lt;&#x2F;p&gt;
&lt;p&gt;The filename is created based on the current datetime of the system, so that we can avoid ambiguity and exactly know how old is the backup file.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;for-docker-compose&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#for-docker-compose&quot; aria-label=&quot;Anchor link for: for-docker-compose&quot;&gt;🔗&lt;&#x2F;a&gt;For docker compose&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############## ✅ Update These Values ##################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;directory&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;your-project-dir&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;home&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;&#x2F;home&#x2F;&amp;lt;user&amp;gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;db_service_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;db&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;db_user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;db_user&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;db_name&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#####################################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############ 🔴 CHANGE WITH CAUTION 🔴 #############&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; +&amp;quot;%Y-%m-%d-%H:%M:%S&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;filename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;$db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;_&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;$date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;.sql&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; $home&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; $directory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec&lt;&#x2F;span&gt;&lt;span&gt; $db_service_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; sh&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;pg_dump -U &lt;&#x2F;span&gt;&lt;span&gt;$db_user $db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$filename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose cp&lt;&#x2F;span&gt;&lt;span&gt; $db_service_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;$filename $home&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;$filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec&lt;&#x2F;span&gt;&lt;span&gt; $db_service_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; rm&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; $home&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#####################################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#####################################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############## ✅ CAN CHANGE BELOW ##################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;aws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; s3 cp&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; s3:&#x2F;&#x2F;your-bucket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;rm&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At the top, you’ve to update variable values based on your project and directory structure, and the main code will use that to backup the database.&lt;&#x2F;p&gt;
&lt;p&gt;At the end of script, we have aws command to copy the backup file to S3, here you need to update bucket name and if you are using other S3 storage provider (other than AWS), then &lt;code&gt;--endpoint-url&lt;&#x2F;code&gt; argument with appropriate value.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;for-native-postgres-installation&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#for-native-postgres-installation&quot; aria-label=&quot;Anchor link for: for-native-postgres-installation&quot;&gt;🔗&lt;&#x2F;a&gt;For Native Postgres Installation&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############## ✅ Update These Values ##################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;home&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;&#x2F;home&#x2F;&amp;lt;user&amp;gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;db_user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;db_user&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;db_name&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#####################################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############ 🔴 CHANGE WITH CAUTION 🔴 ##############&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; +&amp;quot;%Y-%m-%d-%H:%M:%S&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;filename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;$db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;_&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;$date&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;.sql&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;ch&lt;&#x2F;span&gt;&lt;span&gt; $home&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;pg_dump&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span&gt; $db_user $db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;#####################################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;############## ✅ CAN CHANGE BELOW ##################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;aws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; s3 cp&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; s3:&#x2F;&#x2F;urlify-db-backup&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;rm&lt;&#x2F;span&gt;&lt;span&gt; $filename&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When running pg_dump in the native installation, there are two ways postgres will try to authenticate the user.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;auth-peer.html&quot;&gt;Peer based authentication&lt;&#x2F;a&gt;: It will check if the current user (system logged in user) is same as user passed to pg_dump.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If peer auth fails then postgres looks for &lt;code&gt;.pgpass&lt;&#x2F;code&gt; file in home directory for the password.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If both fails then it might ask for password.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;So, make sure you use 1st method and run the script on the same user as the one passed to pg_dump.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup-cron-job&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#setup-cron-job&quot; aria-label=&quot;Anchor link for: setup-cron-job&quot;&gt;🔗&lt;&#x2F;a&gt;Setup Cron Job&lt;&#x2F;h2&gt;
&lt;p&gt;To automate backup and upload of our database, we will use crontab. It comes built-in with almost all the Linux distros and are very easy to setup.&lt;&#x2F;p&gt;
&lt;p&gt;Give your script executable permission:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;chmod&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; +x db_backup.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Open crontab:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;crontab&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will open up crontab in your prefered choice of editor. Use the below statement to run the backup script everyday at 12 PM.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0 12 * * * &#x2F;home&#x2F;&amp;lt;user&amp;gt;&#x2F;db_backup.sh &amp;gt;&amp;gt; cron.log 2&amp;gt;&amp;amp;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Create cron.log file&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;touch&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; cron.log&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Use full path if cron.log is not in current user’s home folder.&lt;&#x2F;p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>How To Take Postgres Database Backup From Docker Container</title>
        <published>2023-10-18T00:00:00+00:00</published>
        <updated>2023-10-18T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/postgres-database-backup-from-docker-container/"/>
        <id>https://vshukla.com/tech/postgres-database-backup-from-docker-container/</id>
        <summary type="html">In this tutorial we are gonna use docker exec and cp command along with pg_dump to take Postgres database backup from docker container.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/postgres-database-backup-from-docker-container/">&lt;p&gt;To take database backup in Postgres we use pg_dump. &lt;code&gt;pg_dump&lt;&#x2F;code&gt; is a utility provided by PostgreSQL to &lt;a href=&quot;&#x2F;tech&#x2F;backup-and-restore-postgresql-database-dump-in-ubuntu&quot;&gt;create the database backup&lt;&#x2F;a&gt;. We are gonna use docker &lt;code&gt;exec&lt;&#x2F;code&gt; and &lt;code&gt;cp&lt;&#x2F;code&gt; command along with pg_dump to take Postgres database backup from docker container.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pg-dump&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#pg-dump&quot; aria-label=&quot;Anchor link for: pg-dump&quot;&gt;🔗&lt;&#x2F;a&gt;pg_dump&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s take a look at how to take backup with &lt;code&gt;pg_dump&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;pg_dump&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; your_db_user&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; your_db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; db_backup.sql&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is straight forward, pass your db username, database name that you want to take backup of and the backup filename which would be created.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;docker-exec&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#docker-exec&quot; aria-label=&quot;Anchor link for: docker-exec&quot;&gt;🔗&lt;&#x2F;a&gt;docker exec&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s do the same thing on Docker, for that we will use &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;engine&#x2F;reference&#x2F;commandline&#x2F;exec&#x2F;&quot;&gt;docker exec&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; exec&lt;&#x2F;span&gt;&lt;span&gt; container_name_or_id sh&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;pg_dump -U your_db_user -d your_db_name &amp;gt; db_backup.sql&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Make sure that the specified container is running, since &lt;code&gt;docker exec&lt;&#x2F;code&gt; command executes on the running container.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;docker-cp&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#docker-cp&quot; aria-label=&quot;Anchor link for: docker-cp&quot;&gt;🔗&lt;&#x2F;a&gt;docker cp&lt;&#x2F;h2&gt;
&lt;p&gt;The docker exec command created the database backup but it’s inside the container file system. So now we need to copy that file from container to our file system.&lt;&#x2F;p&gt;
&lt;p&gt;For that purpose docker gives us &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;engine&#x2F;reference&#x2F;commandline&#x2F;cp&#x2F;&quot;&gt;docker cp&lt;&#x2F;a&gt;, which we can use to copy to&#x2F;from the container’s filesystem.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; cp container_name_or_id:db_backup.sql your_destination&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we have copied our database backup file to our filesystem, it’s time to delete the one from our container. To delete backup file from container do this:&lt;&#x2F;p&gt;
&lt;p&gt;docker exec container_name_or_id rm db_backup.sql&lt;&#x2F;p&gt;
&lt;h2 id=&quot;all-commands-for-docker&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#all-commands-for-docker&quot; aria-label=&quot;Anchor link for: all-commands-for-docker&quot;&gt;🔗&lt;&#x2F;a&gt;All Commands For docker&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; exec container_name_or_id sh&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;pg_dump -U your_db_user -d your_db_name &amp;gt; db_backup.sql&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; cp container_name_or_id:db_backup.sql your_destination&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; exec container_name_or_id rm db_backup.sql&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;docker-compose&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#docker-compose&quot; aria-label=&quot;Anchor link for: docker-compose&quot;&gt;🔗&lt;&#x2F;a&gt;docker compose&lt;&#x2F;h2&gt;
&lt;p&gt;You can run the same commands just use &lt;code&gt;docker compose&lt;&#x2F;code&gt; instead of just &lt;code&gt;docker&lt;&#x2F;code&gt;, and replace container name&#x2F;id with service name.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec service_name sh&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;pg_dump -U your_db_user -d your_db_name &amp;gt; db_backup.sql&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose cp service_name:db_backup.sql your_destination&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;docker&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; compose exec service_name rm db_backup.sql&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
    </entry>
    <entry xml:lang="en">
        <title>How To Store and Serve Private Media Files in Django</title>
        <published>2023-09-14T00:00:00+00:00</published>
        <updated>2023-09-14T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/store-and-serve-private-media-files-in-django/"/>
        <id>https://vshukla.com/tech/store-and-serve-private-media-files-in-django/</id>
        <summary type="html">In this blog post we will see how to use AWS S3 to store and serve private media files in django. S3 provides a layer of security by making object private and accessible to those with the valid presigned url.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/store-and-serve-private-media-files-in-django/">&lt;p&gt;In this blog post we will see how to use &lt;a href=&quot;&#x2F;tags&#x2F;aws-s3&#x2F;&quot;&gt;AWS S3&lt;&#x2F;a&gt; to store and serve private media files in django. S3 provides a layer of security by making object private and accessible to those with the valid &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AmazonS3&#x2F;latest&#x2F;userguide&#x2F;ShareObjectPreSignedURL.html&quot;&gt;presigned url&lt;&#x2F;a&gt; and is only valid for however long owner decides.&lt;&#x2F;p&gt;
&lt;p&gt;We will continue from our last blog post on &lt;a href=&quot;&#x2F;tech&#x2F;store-django-static-and-media-files-in-aws-s3&quot;&gt;How To Store Django Static and Media Files in AWS S3&lt;&#x2F;a&gt;. In this post we will only discuss parts which involves setting up private media.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;storage-backend&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#storage-backend&quot; aria-label=&quot;Anchor link for: storage-backend&quot;&gt;🔗&lt;&#x2F;a&gt;Storage Backend&lt;&#x2F;h2&gt;
&lt;p&gt;Like we had &lt;code&gt;MediaStorage&lt;&#x2F;code&gt; for user uploaded public media files, we will need to create a new storage backend class for Private media files.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;your_project&#x2F;custom_storage.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; PrivateMediaStorage&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;S3Boto3Storage&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;private&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    default_acl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;private&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    file_overwrite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    custom_domain&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;location&lt;&#x2F;strong&gt;: This is the directory in the s3 bucket where all the private files will go. You can change the name as per your need.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;default_acl&lt;&#x2F;strong&gt;: &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AmazonS3&#x2F;latest&#x2F;userguide&#x2F;acls.html&quot;&gt;ACL&lt;&#x2F;a&gt; stands for Access Control List, this enables us to manage access to bucket and objects. Here we are passing &lt;code&gt;private&lt;&#x2F;code&gt; as we want the objects to be private.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;file_overwrite&lt;&#x2F;strong&gt;: We do not want files to be over-written if they have the same name.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;custom_domain&lt;&#x2F;strong&gt;: Earlier we’ve set &lt;code&gt;AWS_S3_CUSTOM_DOMAIN&lt;&#x2F;code&gt; in our settings for static and media url generation, however here we want to generate a signed url so we need to set custom domain to False.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;model&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#model&quot; aria-label=&quot;Anchor link for: model&quot;&gt;🔗&lt;&#x2F;a&gt;Model&lt;&#x2F;h2&gt;
&lt;p&gt;It is better to explicitly define the private media storage class in model fields so that we do not unnecessarily complicate other file fields which uses public MediaStorage.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore we need to explicitly pass the &lt;code&gt;PrivateMediaStorage&lt;&#x2F;code&gt; to the model field to store it as private media and generate signed url when we serve.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;your_project&#x2F;your_app&#x2F;models.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; PrivateFile&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    file&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;FileField&lt;&#x2F;span&gt;&lt;span&gt;(storage&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;PrivateMediaStorage&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; upload_to&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;files&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    uploaded_on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;DateTimeField&lt;&#x2F;span&gt;&lt;span&gt;(auto_now_add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; __str__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;font-style: italic;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;uploaded_on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;file&lt;&#x2F;strong&gt;: This is the field which we are using to point to our private media. You can see we are passing our custom storage class to models.FileField which will now store all our uploads with the settings that we defined for &lt;code&gt;PrivateMediaStorage&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;🔗&lt;&#x2F;a&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;You can use the file field as you usually do, now all the uploaded files will be private. While calling field you will get a presigned url which will allow users to access the file.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;things-to-keep-in-mind&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#things-to-keep-in-mind&quot; aria-label=&quot;Anchor link for: things-to-keep-in-mind&quot;&gt;🔗&lt;&#x2F;a&gt;Things to keep in mind&lt;&#x2F;h2&gt;
&lt;blockquote&gt;
&lt;p&gt;FileField and ImageField in django only stores the location of the file and is created as varchar in database which has max_length of 100. So, if your file location is long then you may want to increase it’s allowed length by passing value to &lt;code&gt;max_length&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;ref&#x2F;models&#x2F;fields&#x2F;#django.db.models.FileField.storage&quot;&gt;FileField&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;The presigned url is only valid for 1 hour by default, you can change this by setting a different value for &lt;code&gt;querystring_expire&lt;&#x2F;code&gt; (in seconds) in PrivateMediaStorage class.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>How To Store Django Static and Media Files in AWS S3</title>
        <published>2023-09-06T00:00:00+00:00</published>
        <updated>2023-09-06T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/store-django-static-and-media-files-in-aws-s3/"/>
        <id>https://vshukla.com/tech/store-django-static-and-media-files-in-aws-s3/</id>
        <summary type="html">In this guide we will use django-storages package to manage our static and media files. Let’s walkthrough the whole process on how to store django static and media files in aws s3.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/store-django-static-and-media-files-in-aws-s3/">&lt;p&gt;In this guide we will use django-storages package to manage our static and media files. Let’s walkthrough the whole process on how to store django static and media files in aws s3.&lt;&#x2F;p&gt;
&lt;p&gt;S3 is a cloud based object storage service provided by AWS (Amazon Web Services), it stands for Simple Storage Service (S3).&lt;&#x2F;p&gt;
&lt;p&gt;Using AWS S3 to serve Static and Media files in Django makes sense since Django is not good at serving those and S3 provides a lot more feature around these.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;s3-bucket&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#s3-bucket&quot; aria-label=&quot;Anchor link for: s3-bucket&quot;&gt;🔗&lt;&#x2F;a&gt;S3 Bucket&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;create-a-bucket&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#create-a-bucket&quot; aria-label=&quot;Anchor link for: create-a-bucket&quot;&gt;🔗&lt;&#x2F;a&gt;Create a bucket&lt;&#x2F;h3&gt;
&lt;p&gt;Let’s create a S3 Bucket where our files will be stored. Few things to keep in mind:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;ACL must be enabled, so that we can control object permission from our Django&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Uncheck all “Block Public Access settings for this bucket” settings and check the acknowledge box&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;figure&gt;
  &lt;img src=&quot;create_s3_bucket.gif&quot; alt=&quot;create s3 bucket&quot; &#x2F;&gt;&lt;figcaption class=&quot;text-sm text-base-content&#x2F;80 text-center mt-2 mb-4&quot;&gt;
      Create S3 bucket
    &lt;&#x2F;figcaption&gt;&lt;&#x2F;figure&gt;
&lt;h3 id=&quot;cors&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#cors&quot; aria-label=&quot;Anchor link for: cors&quot;&gt;🔗&lt;&#x2F;a&gt;CORS&lt;&#x2F;h3&gt;
&lt;p&gt;After bucket is created successfully, now we need to update the CORS settings of the bucket so that if content is being served to different host then your browser might block it.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Open the bucket&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Go to the &lt;strong&gt;Permissions&lt;&#x2F;strong&gt; tab&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;At the end of the page there will be CORS settings&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Put the JSON given below, make sure to put the host which will use the S3 files, you can put multiples.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;    &amp;quot;AllowedHeaders&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;*&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;    &amp;quot;AllowedMethods&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;HEAD&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;GET&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;    &amp;quot;AllowedOrigins&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;yourdomain.com&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;    &amp;quot;ExposeHeaders&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;iam-user&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#iam-user&quot; aria-label=&quot;Anchor link for: iam-user&quot;&gt;🔗&lt;&#x2F;a&gt;IAM User&lt;&#x2F;h2&gt;
&lt;p&gt;An IAM user allow us to access AWS resources programmatically. We need IAM credentials to upload the files to S3.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;create-iam-user&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#create-iam-user&quot; aria-label=&quot;Anchor link for: create-iam-user&quot;&gt;🔗&lt;&#x2F;a&gt;Create IAM User&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Go to IAM page in your AWS Console.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Go to &lt;strong&gt;Users&lt;&#x2F;strong&gt; then &lt;strong&gt;Add users&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Fill out the name that you want to give this user.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Go ahead and create the user, you can skip the permission.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;figure&gt;
  &lt;img src=&quot;create_iam_user.gif&quot; alt=&quot;create iam user&quot; &#x2F;&gt;&lt;figcaption class=&quot;text-sm text-base-content&#x2F;80 text-center mt-2 mb-4&quot;&gt;
      Create IAM user
    &lt;&#x2F;figcaption&gt;&lt;&#x2F;figure&gt;
&lt;h3 id=&quot;setting-up-permission&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#setting-up-permission&quot; aria-label=&quot;Anchor link for: setting-up-permission&quot;&gt;🔗&lt;&#x2F;a&gt;Setting up Permission&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open IAM user which you intend on using.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Make sure you are on &lt;strong&gt;Permissions&lt;&#x2F;strong&gt; (default) tab.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;From “Add permissions” dropdown select “Create inline policy”.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Select &lt;strong&gt;JSON&lt;&#x2F;strong&gt; policy editor instead of Visual and paste below JSON. Make sure to update the bucket name.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Give the policy appropriate name and save the changes.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;  &amp;quot;Version&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;2012-10-17&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;  &amp;quot;Statement&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;      &amp;quot;Effect&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;Allow&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;      &amp;quot;Action&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;s3:*&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;      &amp;quot;Resource&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;arn:aws:s3:::your-bucket-name&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;arn:aws:s3:::your-bucket-name&#x2F;*&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;get-the-access-key&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#get-the-access-key&quot; aria-label=&quot;Anchor link for: get-the-access-key&quot;&gt;🔗&lt;&#x2F;a&gt;Get the access key&lt;&#x2F;h3&gt;
&lt;p&gt;To use the IAM user in our django project we will need to generate access key.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open user page, go to &lt;strong&gt;Security credentials&lt;&#x2F;strong&gt; tab&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Scroll down to &lt;strong&gt;Access keys&lt;&#x2F;strong&gt; section and click on &lt;strong&gt;Create access key&lt;&#x2F;strong&gt; button&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Select “Local code” from the use case menu&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;figure&gt;
  &lt;img src=&quot;iam_user_access_key.gif&quot; alt=&quot;iam user access key&quot; &#x2F;&gt;&lt;figcaption class=&quot;text-sm text-base-content&#x2F;80 text-center mt-2 mb-4&quot;&gt;
      IAM user access key
    &lt;&#x2F;figcaption&gt;&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;setting-up-django&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#setting-up-django&quot; aria-label=&quot;Anchor link for: setting-up-django&quot;&gt;🔗&lt;&#x2F;a&gt;Setting Up Django&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;installing-necessary-packages&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#installing-necessary-packages&quot; aria-label=&quot;Anchor link for: installing-necessary-packages&quot;&gt;🔗&lt;&#x2F;a&gt;Installing necessary packages&lt;&#x2F;h3&gt;
&lt;p&gt;We will be using &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;django-storages&#x2F;&quot;&gt;django-storages&lt;&#x2F;a&gt; package to manage our connection with AWS S3 and to upload our files. We will also need &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;boto3&#x2F;&quot;&gt;boto3&lt;&#x2F;a&gt; package to connect to AWS, which django-storages uses.&lt;&#x2F;p&gt;
&lt;p&gt;pip install boto3 django-storages&lt;&#x2F;p&gt;
&lt;h3 id=&quot;storage-backend&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#storage-backend&quot; aria-label=&quot;Anchor link for: storage-backend&quot;&gt;🔗&lt;&#x2F;a&gt;Storage Backend&lt;&#x2F;h3&gt;
&lt;p&gt;django-storages provides &lt;code&gt;S3Boto3Storage&lt;&#x2F;code&gt; class for media storage and &lt;code&gt;S3StaticStorage&lt;&#x2F;code&gt; for static files.&lt;&#x2F;p&gt;
&lt;p&gt;We will subclass &lt;code&gt;S3Boto3Storage&lt;&#x2F;code&gt; to create our custom storage class so that we can customize certain things.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;your_project&#x2F;custom_storage.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;conf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; settings&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; storages&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;backends&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;s3boto3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; S3Boto3Storage&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; StaticStorage&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;S3Boto3Storage&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; settings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;STATICFILES_LOCATION&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    default_acl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;public-read&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    querystring_auth&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; MediaStorage&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;S3Boto3Storage&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    location&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; settings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIAFILES_LOCATION&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    default_acl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;public-read&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    file_overwrite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;update-settings-py&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#update-settings-py&quot; aria-label=&quot;Anchor link for: update-settings-py&quot;&gt;🔗&lt;&#x2F;a&gt;Update settings.py&lt;&#x2F;h3&gt;
&lt;p&gt;We also need to update our settings.py file with media and static files settings:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;STATICFILES_DIRS&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;    BASE_DIR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;static_assets&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_S3_ACCESS_KEY_ID&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;your_aws_key&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_S3_SECRET_ACCESS_KEY&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;your_aws_secret&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_S3_REGION_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;ap-south-1&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;  # change to your region&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_STORAGE_BUCKET_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;your_bucket_name&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_DEFAULT_ACL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_S3_CUSTOM_DOMAIN&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;{AWS_STORAGE_BUCKET_NAME}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;.s3.amazonaws.com&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# static files&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;STATICFILES_LOCATION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;static&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;STATIC_URL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;https:&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;{AWS_S3_CUSTOM_DOMAIN}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;{STATICFILES_LOCATION}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# media files&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIAFILES_LOCATION&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;media&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIA_URL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;https:&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;{AWS_S3_CUSTOM_DOMAIN}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;{MEDIAFILES_LOCATION}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# storage backends&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;STORAGES&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;    &amp;quot;default&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;BACKEND&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;your_project.custom_storage.MediaStorage&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;  # for media&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;    &amp;quot;staticfiles&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;quot;BACKEND&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;your_project.custom_storage.StaticStorage&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;AWS_S3_OBJECT_PARAMETERS&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;    &amp;quot;CacheControl&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;quot;max-age=2592000&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have done all the setup for our static and media files to be uploaded to our S3 bucket. You can test it out in your django project. You can also checkout the tutorial on &lt;a href=&quot;&#x2F;tech&#x2F;django-rest-framework-file-upload-api&quot;&gt;how to create file upload api using django rest framework&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Django Rest Framework File Upload API</title>
        <published>2023-06-21T00:00:00+00:00</published>
        <updated>2025-08-24T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/django-rest-framework-file-upload-api/"/>
        <id>https://vshukla.com/tech/django-rest-framework-file-upload-api/</id>
        <summary type="html">In this guide we will learn to build Django Rest Framework File Upload API with media storage and using MultiPartParser &amp; FormParser in class based view.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/django-rest-framework-file-upload-api/">&lt;p&gt;Uploading file via API is a very common requirement. In this guide we will learn to build &lt;a href=&quot;&#x2F;tags&#x2F;django-rest-framework&#x2F;&quot;&gt;Django Rest Framework&lt;&#x2F;a&gt; File Upload API.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#setup&quot; aria-label=&quot;Anchor link for: setup&quot;&gt;🔗&lt;&#x2F;a&gt;Setup&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Django: 4.2&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Django Rest Framework: 3.14.0&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Project Name: &lt;code&gt;fileupload&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;App Name: &lt;code&gt;api&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;add-your-app-in-installed-apps&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#add-your-app-in-installed-apps&quot; aria-label=&quot;Anchor link for: add-your-app-in-installed-apps&quot;&gt;🔗&lt;&#x2F;a&gt;Add your app in INSTALLED_APPS&lt;&#x2F;h3&gt;
&lt;p&gt;In &lt;code&gt;fileupload&#x2F;settings.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;INSTALLED_APPS&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;    &amp;#39;rest_framework&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;    &amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt; # your django app&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;update-media-path-in-settings-py&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#update-media-path-in-settings-py&quot; aria-label=&quot;Anchor link for: update-media-path-in-settings-py&quot;&gt;🔗&lt;&#x2F;a&gt;Update MEDIA path in settings.py&lt;&#x2F;h3&gt;
&lt;p&gt;We need to set the path where we want the uploaded files to be stored.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;settings.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIA_URL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;&#x2F;media&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIA_ROOT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; BASE_DIR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;media&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;update-project-s-urls-py-file&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#update-project-s-urls-py-file&quot; aria-label=&quot;Anchor link for: update-project-s-urls-py-file&quot;&gt;🔗&lt;&#x2F;a&gt;Update project’s urls.py file&lt;&#x2F;h3&gt;
&lt;p&gt;For Django to be able to serve media files we need to enable this using &lt;code&gt;static&lt;&#x2F;code&gt; helper function that Django provides. This only works in DEBUG mode and only if folder location is local. &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;4.2&#x2F;howto&#x2F;static-files&#x2F;&quot; target=&quot;_blank&quot;&gt;source&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;fileupload&#x2F;urls.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;conf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; settings&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;conf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;urls&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;static&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; static&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;contrib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; admin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;urls&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; path&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;urlpatterns&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;    path&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;admin&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; admin&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;site&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;urls)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;    path&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;api&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; include&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;api.urls&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; namespace&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;urlpatterns&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; static&lt;&#x2F;span&gt;&lt;span&gt;(settings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIA_URL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; document_root&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;settings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;MEDIA_ROOT&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;model&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#model&quot; aria-label=&quot;Anchor link for: model&quot;&gt;🔗&lt;&#x2F;a&gt;Model&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s create a Model where we store the file path (as &lt;code&gt;file&lt;&#x2F;code&gt;) and file upload time (as &lt;code&gt;uploaded_on&lt;&#x2F;code&gt;). So the uploaded file will be stored in &lt;code&gt;media&lt;&#x2F;code&gt; folder and &lt;code&gt;file&lt;&#x2F;code&gt; field will contain it’s path.&lt;&#x2F;p&gt;
&lt;p&gt;In your &lt;code&gt;api&#x2F;models.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; UploadedFile&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    file&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;FileField&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    uploaded_on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;DateTimeField&lt;&#x2F;span&gt;&lt;span&gt;(auto_now_add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; __str__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;font-style: italic;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;uploaded_on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Run the migration&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; manage.py makemigrations&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; manage.py migrate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;serializer&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#serializer&quot; aria-label=&quot;Anchor link for: serializer&quot;&gt;🔗&lt;&#x2F;a&gt;Serializer&lt;&#x2F;h2&gt;
&lt;p&gt;Creating a ModelSerializer for our Model &lt;code&gt;UploadedFile&lt;&#x2F;code&gt;. ModelSerializer creates serializer fields based on Model passed and comes with buil-in &lt;code&gt;save&lt;&#x2F;code&gt; method that saves passed data into database.&lt;&#x2F;p&gt;
&lt;p&gt;In your &lt;code&gt;api&#x2F;serializers.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; rest_framework&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; serializers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span&gt;models&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; UploadedFile&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; FileUploadSerializer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;serializers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;ModelSerializer&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;    class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; Meta&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        model&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; UploadedFile&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fields&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;file&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;uploaded_on&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;view&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#view&quot; aria-label=&quot;Anchor link for: view&quot;&gt;🔗&lt;&#x2F;a&gt;View&lt;&#x2F;h2&gt;
&lt;p&gt;Now we create the view class &lt;code&gt;FileUploadAPIView&lt;&#x2F;code&gt; (sub-class of APIView) to save the uploaded file. Few things to note:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Generally the &lt;code&gt;JSONParser&lt;&#x2F;code&gt; is set as the default parser in DRF so that we can send the request in JSON format. However it is not advised&#x2F;convenient to upload file in JSON.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;form-data&lt;&#x2F;code&gt; is the best way to upload the file, to enable this we need to pass &lt;code&gt;MultiPartParser&lt;&#x2F;code&gt; and &lt;code&gt;FormParser&lt;&#x2F;code&gt; as our &lt;code&gt;parser_classes&lt;&#x2F;code&gt; in our view.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Only &lt;code&gt;MultiPartParser&lt;&#x2F;code&gt; can be passed to enable file uploading but as per &lt;a href=&quot;https:&#x2F;&#x2F;www.django-rest-framework.org&#x2F;api-guide&#x2F;parsers&#x2F;#multipartparser&quot; target=&quot;_blank&quot;&gt;DRF API Guide on MultiPartParser&lt;&#x2F;a&gt;, we should use both parser classes to fully support HTML form data.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In &lt;code&gt;api&#x2F;views.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; rest_framework&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; status&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; rest_framework&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;parsers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; FormParser&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; MultiPartParser&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; rest_framework&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;response&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Response&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; rest_framework&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;views&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; APIView&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span&gt;serializers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; FileUploadSerializer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt; FileUploadAPIView&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;&quot;&gt;APIView&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    parser_classes&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (MultiPartParser&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; FormParser)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    serializer_class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; FileUploadSerializer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; post&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt; request&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; **&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;kwargs&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        serializer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #39BAE6;font-style: italic;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;serializer_class&lt;&#x2F;span&gt;&lt;span&gt;(data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; serializer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;is_valid&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;            # you can access the file like this from serializer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;            # uploaded_file = serializer.validated_data[&amp;quot;file&amp;quot;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            serializer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;save&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; Response&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                serializer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                status&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;status&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;HTTP_201_CREATED&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; Response&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            serializer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;errors&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            status&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;status&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt;HTTP_400_BAD_REQUEST&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;urls&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#urls&quot; aria-label=&quot;Anchor link for: urls&quot;&gt;🔗&lt;&#x2F;a&gt;Urls&lt;&#x2F;h2&gt;
&lt;p&gt;Let’s connect our view in urls to be able to receive requests on it.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;api&#x2F;urls.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; django&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;urls&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; path&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span&gt;views&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; FileUploadAPIView&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ImageUploadAPIView&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;urlpatterns&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;    path&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;upload-file&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; FileUploadAPIView&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;as_view&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;upload-file&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;testing-api-endpoint&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#testing-api-endpoint&quot; aria-label=&quot;Anchor link for: testing-api-endpoint&quot;&gt;🔗&lt;&#x2F;a&gt;Testing API Endpoint&lt;&#x2F;h2&gt;
&lt;p&gt;Start the server&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; manage.py runserver&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;postman&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#postman&quot; aria-label=&quot;Anchor link for: postman&quot;&gt;🔗&lt;&#x2F;a&gt;Postman&lt;&#x2F;h3&gt;
&lt;figure&gt;
  &lt;img src=&quot;drf-file-upload-postman.png&quot; alt=&quot;drf file upload postman&quot; &#x2F;&gt;&lt;figcaption class=&quot;text-sm text-base-content&#x2F;80 text-center mt-2 mb-4&quot;&gt;
      File upload using postman
    &lt;&#x2F;figcaption&gt;&lt;&#x2F;figure&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Method: POST (as per our view)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;URL: http:&#x2F;&#x2F;localhost:8000&#x2F;api&#x2F;upload-file&#x2F;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Body Type: form-data&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Values to be passed&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;file as KEY (change the type to File in dropdown), Browse and select a file for VALUE&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;curl&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#curl&quot; aria-label=&quot;Anchor link for: curl&quot;&gt;🔗&lt;&#x2F;a&gt;cURL&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;curl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; --location --request&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; POST &amp;#39;http:&#x2F;&#x2F;localhost:8000&#x2F;api&#x2F;upload-file&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; --form&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;file=@&amp;quot;&#x2F;path&#x2F;to&#x2F;yourfile.pdf&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;nodejs-axios&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#nodejs-axios&quot; aria-label=&quot;Anchor link for: nodejs-axios&quot;&gt;🔗&lt;&#x2F;a&gt;NodeJS - Axios&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; axios&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;axios&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; FormData&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;form-data&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; fs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;fs&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt; FormData&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;file&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; fs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;createReadStream&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt;&amp;#39;&#x2F;path&#x2F;to&#x2F;yourfile.pdf&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;var&lt;&#x2F;span&gt;&lt;span&gt; config&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  method&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;post&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; &amp;#39;http:&#x2F;&#x2F;localhost:8000&#x2F;api&#x2F;upload-file&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  headers&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;    ...&lt;&#x2F;span&gt;&lt;span&gt;data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;getHeaders&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;axios&lt;&#x2F;span&gt;&lt;span&gt;(config)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;response&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    console&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(JSON&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;stringify&lt;&#x2F;span&gt;&lt;span&gt;(response&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;catch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FF8F40;&quot;&gt;function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D2A6FF;&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    console&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FFB454;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(error)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  })&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Checkout this guide to learn &lt;a href=&quot;&#x2F;tech&#x2F;store-django-static-and-media-files-in-aws-s3&quot;&gt;how to store static and media files in AWS S3&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>Backup and Restore PostgreSQL database dump in Ubuntu</title>
        <published>2023-06-20T00:00:00+00:00</published>
        <updated>2023-06-20T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/backup-and-restore-postgresql-database-dump-in-ubuntu/"/>
        <id>https://vshukla.com/tech/backup-and-restore-postgresql-database-dump-in-ubuntu/</id>
        <summary type="html">Learn how to backup and restore PostgreSQL database dump in Ubuntu (Linux) and Mac.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/backup-and-restore-postgresql-database-dump-in-ubuntu/">&lt;p&gt;Learn how to backup and restore &lt;a href=&quot;&#x2F;tags&#x2F;postgresql&#x2F;&quot;&gt;PostgreSQL&lt;&#x2F;a&gt; database dump in Ubuntu (Linux) and Mac.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;taking-backup-dump-of-the-database&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#taking-backup-dump-of-the-database&quot; aria-label=&quot;Anchor link for: taking-backup-dump-of-the-database&quot;&gt;🔗&lt;&#x2F;a&gt;Taking backup&#x2F;dump of the database:&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;pg_dump&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; db_user database_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; database_dump_name.sql&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;restoring-backup-dump&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#restoring-backup-dump&quot; aria-label=&quot;Anchor link for: restoring-backup-dump&quot;&gt;🔗&lt;&#x2F;a&gt;Restoring backup dump&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;creating-new-user&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#creating-new-user&quot; aria-label=&quot;Anchor link for: creating-new-user&quot;&gt;🔗&lt;&#x2F;a&gt;Creating new user&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;CREATE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; USER your_user_name WITH PASSWORD &amp;#39;your_user_password&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;creating-new-database&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#creating-new-database&quot; aria-label=&quot;Anchor link for: creating-new-database&quot;&gt;🔗&lt;&#x2F;a&gt;Creating new database&lt;&#x2F;h3&gt;
&lt;p&gt;It is better to switch to new user and create the new database, this way you won’t have to give permission explicitly.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;CREATE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; DATABASE your_db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But for some reason you didn’t&#x2F;couldn’t create the database with new user then go to the last section which has all the commands to give user full access to the database.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;restore-using-psql&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#restore-using-psql&quot; aria-label=&quot;Anchor link for: restore-using-psql&quot;&gt;🔗&lt;&#x2F;a&gt;Restore using psql&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;psql&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #95E6CB;&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; your_db_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F29668;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; dump_name.sql&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;give-full-access-to-database&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#give-full-access-to-database&quot; aria-label=&quot;Anchor link for: give-full-access-to-database&quot;&gt;🔗&lt;&#x2F;a&gt;Give Full Access to Database&lt;&#x2F;h2&gt;
&lt;p&gt;If you haven’t created database with your new user then you’ll have to give it full permission so that it can perform the operations.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BFBDB6; background-color: #0D1017;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;GRANT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; ALL PRIVILEGES ON DATABASE your_db_name TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# change owner&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;ALTER&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; DATABASE your_db_name OWNER TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# allow user to connect&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;GRANT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; CONNECT ON DATABASE your_db_name TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# grant public schema access&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;GRANT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; USAGE ON SCHEMA public TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #5A6673;font-style: italic;&quot;&gt;# grant access to all tables and sequences in schma public&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;GRANT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #59C2FF;&quot;&gt;GRANT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AAD94C;&quot;&gt; ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO your_user_name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #BFBDB6B3;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Reference: &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;backup.html&quot;&gt;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;backup.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
    </entry>
</feed>
