<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sapan Bodiwala</title>
    <description>The latest articles on DEV Community by Sapan Bodiwala (@sbodi10).</description>
    <link>https://dev.to/sbodi10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F157021%2Fd5c78ef0-aee8-4287-9e6e-45f88f27c116.jpeg</url>
      <title>DEV Community: Sapan Bodiwala</title>
      <link>https://dev.to/sbodi10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sbodi10"/>
    <language>en</language>
    <item>
      <title>Download Images using JavaScript 📸</title>
      <dc:creator>Sapan Bodiwala</dc:creator>
      <pubDate>Wed, 03 Mar 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/sbodi10/download-images-using-javascript-51a9</link>
      <guid>https://dev.to/sbodi10/download-images-using-javascript-51a9</guid>
      <description>&lt;p&gt;Let's say you're trying to add a feature to your site or app to allow users to download an image upon clicking a button. Should be pretty straight forward right? I thought so myself as I needed to add this feature when building an internal media library tool at &lt;a href="https://www.discovery.com/"&gt;Discovery&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I tried adding this feature by just adding the &lt;strong&gt;download&lt;/strong&gt; attribute to an anchor tag and setting the &lt;strong&gt;href&lt;/strong&gt; attribute to the image url (cross-origin url).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'URL_HERE'&lt;/span&gt; &lt;span class="na"&gt;download&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Download image&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Turns out that Chrome started &lt;a href="https://developers.google.com/web/updates/2018/02/chrome-65-deprecations#block_cross-origin_a_download"&gt;ignoring&lt;/a&gt; download attributes that pointed to cross origin urls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To solve this&lt;/strong&gt;, we'll take advantage of the browser built in &lt;strong&gt;fetch&lt;/strong&gt; method. You'll need to add an event listener to the element that you want to trigger the image download and call the below function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using fetch&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;downloadImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageSrc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageSrc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imageBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imageURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;imageURL&lt;/span&gt;
  &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image file name here&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;imageSrc&lt;/strong&gt; function parameter represents the cross origin image url.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"&gt;fetch&lt;/a&gt; to get the &lt;em&gt;ReadableStream&lt;/em&gt; data of the image&lt;/li&gt;
&lt;li&gt;Next, we call the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/blob"&gt;blob&lt;/a&gt; method provided by &lt;strong&gt;fetch&lt;/strong&gt; to get the raw image data&lt;/li&gt;
&lt;li&gt;Third, we use the &lt;strong&gt;URL Web API&lt;/strong&gt; to call the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL"&gt;createObjectURL&lt;/a&gt; static method to create a URL that represents the image's download URL&lt;/li&gt;
&lt;li&gt;Finally, an anchor element is created to set the new url to the &lt;strong&gt;href&lt;/strong&gt; attribute. You can also set the name of the file by setting the &lt;strong&gt;download&lt;/strong&gt; attribute of the anchor element. Last, we append the anchor element we created to the DOM and trigger a click to download the image and then quickly remove the anchor from the document.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And there you go! You now have the ability to download images on the trigger of a function!&lt;/p&gt;

&lt;p&gt;I hope this helped and was useful for you!&lt;/p&gt;

&lt;p&gt;This post can also be found at &lt;a href="https://sapanbodiwala.com/blog"&gt;sapanbodiwala.com/blog&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>download</category>
      <category>images</category>
    </item>
    <item>
      <title>Deploying to AWS Amplify 🔊</title>
      <dc:creator>Sapan Bodiwala</dc:creator>
      <pubDate>Thu, 28 Jan 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/sbodi10/deploying-to-aws-amplify-3jhh</link>
      <guid>https://dev.to/sbodi10/deploying-to-aws-amplify-3jhh</guid>
      <description>&lt;p&gt;If you're trying to &lt;strong&gt;deploy&lt;/strong&gt; your front-end app to &lt;strong&gt;AWS&lt;/strong&gt; using their &lt;strong&gt;Amplify&lt;/strong&gt; service, but your builds keep failing, you've come to the right place. I struggled trying to get this to work with no clear documentation from AWS explaining how to set up deployments from a private repository.&lt;/p&gt;

&lt;p&gt;After several &lt;a href="https://github.com/aws-amplify/amplify-console/issues/157"&gt;stackoverflow searches&lt;/a&gt; and contacting AWS, I decided to write this blog post to help others solve their deployment issues. The steps explained below are written as if you were starting from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Login to your AWS Management console. Then navigate over to the AWS Amplify service. Next, click on the &lt;strong&gt;New app&lt;/strong&gt; dropdown button and select &lt;strong&gt;Host web app&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Select your repository host provider (in my case I was using Bitbucket) and then click the &lt;strong&gt;Continue&lt;/strong&gt; button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Authenticate with your repository host provider. Once authenticated, select the repo / app that you would like to deploy in the dropdown under &lt;strong&gt;Recently updated repositories&lt;/strong&gt;. &lt;em&gt;If you don't see your repo listed, you'll likely need Admin access or you'll need to push changes to the app&lt;/em&gt;. Then select the branch you would like to get deployed each time you push a change to that branch. Click on the &lt;strong&gt;Next&lt;/strong&gt; button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;p&gt;You should now see &lt;strong&gt;Configure build settings&lt;/strong&gt; at the top. Feel free to update the application name at the top if you'd like. Then, we'll need to update the list of commands under &lt;strong&gt;preBuild&lt;/strong&gt; in the &lt;strong&gt;Build and test settings&lt;/strong&gt; section.&lt;/p&gt;

&lt;p&gt;We'll want to add the these two lines before "- yarn install"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;eval "$(ssh-agent -s)"&lt;/li&gt;
&lt;li&gt;ssh-add &amp;lt;(echo "$DEPLOYMENT_KEY" | base64 -d)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We're adding the two lines above to tell AWS Amplify to use the ssh-agent to evaluate our ssh key that we will add below.&lt;/p&gt;

&lt;p&gt;So your file should look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="na"&gt;frontend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;phases&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;preBuild&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;commands&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;eval "$(ssh-agent -s)"&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ssh-add &amp;lt;(echo "$DEPLOYMENT_KEY" | base64 -d)&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;yarn install&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;commands&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;yarn run build&lt;/span&gt;
  &lt;span class="na"&gt;artifacts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# IMPORTANT - Please verify your build output directory&lt;/span&gt;
    &lt;span class="na"&gt;baseDirectory&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
    &lt;span class="na"&gt;files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;**/*'&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;node_modules/**/*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: It's important to double check your build to see what the output directory is and update the &lt;strong&gt;baseDirectory&lt;/strong&gt; property above as needed&lt;/p&gt;

&lt;p&gt;Next, you'll want to click on the &lt;strong&gt;Advanced settings&lt;/strong&gt; dropdown.&lt;/p&gt;

&lt;p&gt;Under environment variables, you'll want to add an environment variable for &lt;strong&gt;DEPLOYMENT_KEY&lt;/strong&gt;, which we added to the build configuration above. Now that you've entered in &lt;strong&gt;DEPLOYMENT_KEY&lt;/strong&gt; in the &lt;strong&gt;Key&lt;/strong&gt; field, let's move onto the next step to get the value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5
&lt;/h2&gt;

&lt;p&gt;We'll need to generate an SSH key for both Amplify and your repository provider.&lt;/p&gt;

&lt;p&gt;First, create the SSH key pair without a password by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-f&lt;/span&gt; DEPLOYMENT_KEY &lt;span class="nt"&gt;-N&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, we'll need to base64 encode the SSH key and &lt;strong&gt;copy the output&lt;/strong&gt; into the value field for our DEPLOYMENT_KEY environment variable in the Amplify Console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;DEPLOYMENT_KEY | &lt;span class="nb"&gt;base64&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last, we'll need to add the contents of the DEPLOYMENT_KEY.pub to the access keys of the private repository that you want to deploy. This will vary based on the repository provider. You should be able to find how to add the public key to your repository provider in the settings of either the repo or your account settings.&lt;/p&gt;

&lt;p&gt;After adding the private key to the value field in the environment variable settings and adding the public key to your repository provider, click on the &lt;strong&gt;Next&lt;/strong&gt; button and then &lt;strong&gt;Save and deploy&lt;/strong&gt;. Your first build will now begin 🥳.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congrats on deploying to AWS Amplify! I hope that worked for you! Please reach out if you run into any issues along the way!&lt;/p&gt;

&lt;p&gt;This post can also be found at &lt;a href="https://sapanbodiwala.com/blog/deploying-to-aws-amplify"&gt;sapanbodiwala.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deployment</category>
      <category>aws</category>
      <category>amplify</category>
      <category>help</category>
    </item>
  </channel>
</rss>
