<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rohit Taneja on Medium]]></title>
        <description><![CDATA[Stories by Rohit Taneja on Medium]]></description>
        <link>https://medium.com/@rohittaneja?source=rss-5510cddb8143------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xFz3PN6O3sHOiyGUla04Fw.jpeg</url>
            <title>Stories by Rohit Taneja on Medium</title>
            <link>https://medium.com/@rohittaneja?source=rss-5510cddb8143------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 10:28:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rohittaneja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Client-side file upload or server-side file upload. Which one is better?]]></title>
            <link>https://medium.com/1mgofficial/client-side-file-upload-or-server-side-file-upload-which-one-is-better-d431926df950?source=rss-5510cddb8143------2</link>
            <guid isPermaLink="false">https://medium.com/p/d431926df950</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[image]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Rohit Taneja]]></dc:creator>
            <pubDate>Thu, 16 Apr 2020 09:53:43 GMT</pubDate>
            <atom:updated>2020-04-16T10:08:42.816Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sF3-EZRcXW05x4Q_" /><figcaption>Photo by <a href="https://unsplash.com/@marcojodoin?utm_source=medium&amp;utm_medium=referral">Marc-Olivier Jodoin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We at 1mg have moved some of our server-side file uploads to the client-side.</p><p>In this article, I’ll discuss the difference between server-side and client-side file upload and why client-side is a better choice.</p><h3>What is a server-side file upload?</h3><p><em>Server-side means that the action i.e file upload takes place on the application server</em></p><p><em>Client i.e the browser(Chrome) or Apps (Android, iOS) uploads the file content to the website’s server</em></p><p><em>Server uploads the file content to a third party file storage utility (AWS S3)</em></p><h3>What is a client-side file upload?</h3><p><em>Client-side means that the action is done on the user’s device</em></p><p><em>Client requests for presigned URL data with authentication info from server</em></p><p><em>Client uploads the file to AWS S3 with the above authentication info</em></p><p><em>Client sends the file URL received from the above S3 request to the website’s server</em></p><h3>A file upload request flow:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*oGlWZHhivc8ci4sIZI5Uhg.png" /><figcaption>A file upload request flow</figcaption></figure><h3>Pros of client-side approach:</h3><p>As we don’t have the file data and have just the URL of the file,<br>we don’t have to carry the file data across multiple hops which has the following pros:</p><ul><li>Lower memory consumption</li><li>Lower CPU consumption</li><li>Lower file read/write resources consumption</li><li>Faster uploads as uploads now will go directly to S3</li><li>Cheaper as outbound traffic from the server accrues charges</li></ul><h3>Cons of client-side approach:</h3><ul><li>It comes with some security concerns discussed in the next section</li><li>Number of API requests from client are 3 now instead of 1 in server-side</li><li>Clients will also have to make changes if in future we want to change the third party file utility from S3 to others. This becomes difficult in case of apps (Android, iOS) as in this cases users will have to update the app to get new changes</li></ul><h3>Security concerns in client-side file upload and their solutions:</h3><p><strong>Concern: User trying to upload a huge file (say size ≥ 20MB)<br></strong>Solution: We can set file size limit in the presigned URL</p><p><strong>Concern: User trying to upload a different file type</strong><br>Solution: We can define allowed file type in presigned URL. Example jpeg, png, etc</p><p><strong>Concern: Presigned URL credentials got leaked or being used multiple times<br></strong>Solution: We can set expiry time of the presigned URL</p><p><strong>Concern: User is abusing presigned URL get API and uploading files to AWS S3<br></strong>Solution: We can rate-limit user in the presigned URL API call</p><h3>Some important points to consider:</h3><ul><li>A catch that got me half a day to figure out was that AWS API expects file parameter to be present at the end of the HTTP request and if we send it before the end then AWS ignores all the parameters after the file parameter</li><li>The response of this curl request will be in XML and there is no way to get the response in JSON. So we have to convert the response from XML to JSON if required</li><li>If you have multiple files to upload, you can upload them parallelly to save time</li></ul><h3>How to get started</h3><ul><li><a href="https://www.python.org/downloads/release/python-376/">Install Python 3.7</a></li><li><a href="https://docs.python.org/3.7/tutorial/venv.html">Create a virtual environment</a> so that you don’t mess with your existing setup</li></ul><pre>python3.7 -m venv new_virtual_env</pre><ul><li><a href="https://pypi.org/project/boto3/">Install boto3</a> : AWS package for python</li></ul><pre>pip3 install boto3</pre><ul><li>Install requests package for making HTTP requests</li></ul><pre>pip3 install requests</pre><ul><li><a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/">Create AWS account</a>: <a href="https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&amp;all-free-tier.sort-order=asc">The free tier usage</a> of S3 is more than enough for testing</li><li>Create a new bucket in S3<br>You’ll have to set setting for <strong>Block <em>all</em> public access </strong>OFF if you want the files to be publicly accessible</li><li>As a security measure you can add CORS policy to allow only your domains from web browsers to make PUT/POST HTTP requests to S3</li></ul><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;CORSConfiguration xmlns=&quot;<a href="http://s3.amazonaws.com/doc/2006-03-01/">http://s3.amazonaws.com/doc/2006-03-01/</a>&quot;&gt;<br>&lt;CORSRule&gt;<br>    &lt;AllowedOrigin&gt;<a href="https://www.yourdomain.com/">https://www.yourdomain.com</a>&lt;/AllowedOrigin&gt;<br>    &lt;AllowedMethod&gt;PUT&lt;/AllowedMethod&gt;<br>    &lt;AllowedMethod&gt;POST&lt;/AllowedMethod&gt;<br>    &lt;AllowedHeader&gt;*&lt;/AllowedHeader&gt;<br>&lt;/CORSRule&gt;<br>&lt;CORSRule&gt;<br>    &lt;AllowedOrigin&gt;*&lt;/AllowedOrigin&gt;<br>    &lt;AllowedMethod&gt;GET&lt;/AllowedMethod&gt;<br>&lt;/CORSRule&gt;<br>&lt;/CORSConfiguration&gt;</pre><ul><li><a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-access-key/">Create Access keys</a> for S3 upload</li><li>Python code snippet to get presigned url data and to test presigned upload</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94092f0829ef0323157e9018244b636b/href">https://medium.com/media/94092f0829ef0323157e9018244b636b/href</a></iframe><ul><li>Sample Curl request for uploading file from Presigned data:</li></ul><pre>curl -L -X POST &#39;<a href="https://test.s3.amazonaws.com/&#39;">https://test.s3.amazonaws.com/&#39;</a> -H &#39;Content-Type: application/x-www-form-urlencoded&#39; -F &#39;Content-Type=Content-Type&#39; -F &#39;acl=acl&#39; -F &#39;key=key&#39; -F &#39;policy=policy&#39; -F &#39;success_action_status=success_action_status&#39; -F &#39;x-amz-algorithm=x-amz-algorithm&#39; -F &#39;x-amz-credential=x-amz-credential&#39; -F &#39;x-amz-date=x-amz-date&#39; -F &#39;x-amz-signature=x-amz-signature&#39; -F &#39;file=@/path_to_testfile.png&#39;</pre><h3><strong>Conclusion:</strong></h3><p>Although client-side approach has cons but they either have workarounds or are not of much impact so the pros overweigh them and hence client-side approach is a better approach.</p><h4>Thanks for stopping by.</h4><p>I hope you enjoyed the read and learned something.<br>Please feel free to share your views and feedback in the comments section.</p><h4>If you found this helpful, please clap and share the article. Let other people also learn from this article.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d431926df950" width="1" height="1" alt=""><hr><p><a href="https://medium.com/1mgofficial/client-side-file-upload-or-server-side-file-upload-which-one-is-better-d431926df950">Client-side file upload or server-side file upload. Which one is better?</a> was originally published in <a href="https://medium.com/1mgofficial">Tata 1mg Technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>