<?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: Uri Shaked</title>
    <description>The latest articles on DEV Community by Uri Shaked (@urishaked).</description>
    <link>https://dev.to/urishaked</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%2F155323%2F74f7eaef-67e7-42de-9fdb-6eacd8092121.jpg</url>
      <title>DEV Community: Uri Shaked</title>
      <link>https://dev.to/urishaked</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/urishaked"/>
    <language>en</language>
    <item>
      <title>Exporting Google Cloud Storage File List to BigQuery (CSV)</title>
      <dc:creator>Uri Shaked</dc:creator>
      <pubDate>Thu, 30 Jul 2020 12:18:36 +0000</pubDate>
      <link>https://dev.to/urishaked/exporting-google-cloud-storage-file-list-to-bigquery-csv-41l6</link>
      <guid>https://dev.to/urishaked/exporting-google-cloud-storage-file-list-to-bigquery-csv-41l6</guid>
      <description>&lt;p&gt;Sometimes you need to run complex queries against the list of files storage in your Google Cloud Storage. In my case, I wanted find out which files were no longer referenced by my database and clean them up.&lt;/p&gt;

&lt;p&gt;Here is how I did it:&lt;/p&gt;

&lt;h2&gt;
  
  
  Exporting the File List to a CSV file
&lt;/h2&gt;

&lt;p&gt;This simple 1-line script will allow you to the entire file list from Google Cloud Storage to a simple CSV format that can be then loaded into Google BigQuery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;gsutil &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; gs://bucket-name/&lt;span class="k"&gt;**&lt;/span&gt; | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n-1&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'BEGIN{print "Size,Modified,Path"}{print $1","$2",\""$3"\""}'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; filelist.csv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure to replace &lt;code&gt;bucket-name&lt;/code&gt; with your actual GCS bucket name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Loading the CSV file to BigQuery
&lt;/h2&gt;

&lt;p&gt;Before you can load the CSV File to Google's BigQuery, you first need to upload it to Google Cloud storage. You can do it by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;gsutil &lt;span class="nb"&gt;cp &lt;/span&gt;filelist.csv gs://some-bucket/filelist.csv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, you can load it right into bigquery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;bq load &lt;span class="nt"&gt;--autodetect&lt;/span&gt; &lt;span class="nt"&gt;--source_format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;CSV mydataset.mytable gs://some-bucket/filelist.csv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure you change &lt;code&gt;some-bucket&lt;/code&gt; to your actual bucket name, and &lt;code&gt;mydataset&lt;/code&gt; and &lt;code&gt;mytable&lt;/code&gt; to the target BigQuery dataset and table, respectively.&lt;/p&gt;

&lt;p&gt;I tested this process for a bucket containing about 350,000 files and it worked flawlessly.&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>bigquery</category>
      <category>gsutil</category>
    </item>
    <item>
      <title>!x &amp;&amp; (x !== false) &amp;&amp; (x !== 0) &amp;&amp; (x !== '') &amp;&amp; (x !== null) &amp;&amp; (x !== undefined)</title>
      <dc:creator>Uri Shaked</dc:creator>
      <pubDate>Tue, 23 Jul 2019 16:18:48 +0000</pubDate>
      <link>https://dev.to/urishaked/x-x-false-x-0-x-x-null-x-undefined-22fn</link>
      <guid>https://dev.to/urishaked/x-x-false-x-0-x-x-null-x-undefined-22fn</guid>
      <description>&lt;p&gt;JavaScript never ceases to surprise me. This time, a friend shared the following riddle in the JavaScript Israel community:&lt;/p&gt;

&lt;p&gt;Which value of &lt;code&gt;x&lt;/code&gt; will cause the following statement to be &lt;code&gt;true&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Basically, we want to find a value that is falsy in JavaScript (in other words, &lt;code&gt;!x&lt;/code&gt; is true), but is not &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, zero or the empty string.&lt;/p&gt;

&lt;p&gt;Take a moment to think of an answer. Then, scroll down for my answer and other answers suggested by the community.&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h2&gt;
  
  
  My Answer
&lt;/h2&gt;

&lt;p&gt;My approach was to make &lt;code&gt;x&lt;/code&gt; evaluate to a different value each time, so it will be falsy when &lt;code&gt;!x&lt;/code&gt; is evaluated, but will pass all the other tests. &lt;/p&gt;

&lt;p&gt;Thus, I defined a getter for x on the &lt;code&gt;window&lt;/code&gt; object, incrementing the returned value upon each call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;x&lt;/code&gt; starts with zero and increments on each read, the original expression becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;which evaluates to true!&lt;/p&gt;

&lt;h2&gt;
  
  
  Even More Answers
&lt;/h2&gt;

&lt;p&gt;While I was typing my answer, someone has already found a very simple answer: x should be &lt;code&gt;NaN&lt;/code&gt;. This value is falsy, and is indeed not equal to any of the the other values in the expression. You can also verify this by looking at the &lt;a href="https://www.ecma-international.org/ecma-262/6.0/#sec-toboolean"&gt;definition of toBoolean&lt;/a&gt; in the ECMAScript standard.&lt;/p&gt;

&lt;p&gt;Another answer, which really surprised me, was &lt;code&gt;document.all&lt;/code&gt;. If you are curious to know why &lt;code&gt;document.all&lt;/code&gt; is indeed falsy, check out &lt;a href="https://stackoverflow.com/a/10394873/830623"&gt;this StackOverflow answer&lt;/a&gt;. Legacy reasons, amazing!&lt;/p&gt;

&lt;p&gt;Finally, the answer that the original author of this riddle was looking for, is &lt;code&gt;BigInt(0)&lt;/code&gt;. If you haven't heard of this before, don't worry - it's still not an official part of the JavaScript language, but will probably be soon, as part of the &lt;a href="https://github.com/tc39/proposal-bigint"&gt;BigInt proposal&lt;/a&gt;. If you are interested, MDN has &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"&gt;a pretty good documentation about BigInt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What I love about this riddle, is how diverse the solutions are: one exploits the dynamic nature of JavaScript using &lt;code&gt;Object.defineProperty&lt;/code&gt;, another solution uses legacy feature (document.all), while the &lt;code&gt;BigInt&lt;/code&gt; solution uses a feature which has not yet introduced into the language (but is already available in Chrome). Pure fun, isn't it?&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>cURL, the Cloud, 20GB files and I!</title>
      <dc:creator>Uri Shaked</dc:creator>
      <pubDate>Sun, 30 Jun 2019 09:21:41 +0000</pubDate>
      <link>https://dev.to/urishaked/curl-the-cloud-20gb-files-and-i-7g9</link>
      <guid>https://dev.to/urishaked/curl-the-cloud-20gb-files-and-i-7g9</guid>
      <description>&lt;p&gt;A quick tip how to use the cloud to transfer humongous files between different cloud services using cURL!&lt;/p&gt;

&lt;p&gt;For the past few years, I have been co-organizing several meetups, including &lt;a href="https://www.meetup.com/JavaScript-Israel/" rel="noopener noreferrer"&gt;JavaScript Israel&lt;/a&gt; and &lt;a href="https://www.meetup.com/IoT-Makers-Israel/" rel="noopener noreferrer"&gt;IoT Makers Israel&lt;/a&gt;. I am a firm believer in recording all the talks and sharing them on YouTube/Vimeo. &lt;/p&gt;

&lt;p&gt;For some reason, the video guys sometimes like to share the raw video files using random file sharing services such as sendgb.com, filemail.com, and similar. These files are usually several gigabytes large, and these services will only keep them for a few days.&lt;/p&gt;

&lt;p&gt;Since my home upload is not very fast, I was looking for a quick solution that would enable me to transfer these files to a different location, such as google cloud storage or Vimeo, without having to download them to my machine first and then re-upload them to their new home.&lt;/p&gt;

&lt;p&gt;After some fiddling, I found a nice trick that makes this super-simple: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I go to the file sharing service website in Chrome on my machine, and open the developer tools. Then I click on "Download".&lt;/li&gt;
&lt;li&gt;I look at the "Network" tab of Chrome's developer tools, and find the specific line that was triggered by my download request. I right click on it, choose "Copy" and "Copy as cURL (bash)". The copied command includes all the relevant cookies, headers, and any submitted form data, making it easy to run the same HTTP request on a different machine and get the same file. &lt;/li&gt;
&lt;li&gt;Finally, I SSH to one of my cloud machines, paste the cURL command that I just copied, and pipe its output to a second command that will upload the file to the desired service. See some examples below.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytzq1rrbklpebuofx0f1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytzq1rrbklpebuofx0f1.png" alt="Copy as cURL(bash)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Copying the file to Google Cloud Storage or AWS
&lt;/h2&gt;

&lt;p&gt;For copying the file to GCS, I use the &lt;code&gt;gsutil&lt;/code&gt; command from any Google Cloud VM (the VM needs to have write permissions for GCS), or for smaller files (&amp;lt;3GB), I just fire up the &lt;a href="https://cloud.google.com/shell/" rel="noopener noreferrer"&gt;Cloud Shell&lt;/a&gt;. The result looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl ... | gsutil cp - gs://bucket-name/filename.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Where &lt;code&gt;curl ...&lt;/code&gt; is the command you copied from Chrome Devtools, &lt;code&gt;bucket-name&lt;/code&gt; is the target GCS bucket you want to copy that file to, and &lt;code&gt;filename.mp4&lt;/code&gt; is what you want to call the file.&lt;/p&gt;

&lt;p&gt;If you work on AWS, you can use the &lt;code&gt;aws s3&lt;/code&gt; command to achieve a similar result:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl ... | aws s3 cp - s3://bucket-name/filename.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Uploading the file directly to Vimeo
&lt;/h2&gt;

&lt;p&gt;I recently found out that Vimeo &lt;a href="https://vimeo.zendesk.com/hc/en-us/articles/229504987-Uploading-via-FTP" rel="noopener noreferrer"&gt;support FTP uploads on their paid plans&lt;/a&gt;. Thus, I use a similar method, but this time I pipe the output of &lt;code&gt;curl&lt;/code&gt; into a different &lt;code&gt;curl&lt;/code&gt; process that uploads it through FTP:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl ... | curl -T - ftp://user:PASS@ftp-3.cloud.vimeo.com/video.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can use this method to upload the file to any ftp server - just replace the &lt;code&gt;user&lt;/code&gt;, &lt;code&gt;PASS&lt;/code&gt; and &lt;code&gt;ftp-3.cloud.vimeo.com&lt;/code&gt; with the relevant values for your FTP account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uploading to other places - Google Drive, Dropbox, etc.
&lt;/h2&gt;

&lt;p&gt;You can easily extend this method and upload the files to your favorite cloud provider. For instance, for Google Drive, you can use &lt;a href="https://github.com/gdrive-org/gdrive#downloads" rel="noopener noreferrer"&gt;the gdrive cli&lt;/a&gt; and pipe the cURL output to &lt;code&gt;gdrive upload - &amp;lt;path&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;For Dropbox, you can follow the steps &lt;a href="https://stackoverflow.com/a/42120964/830623" rel="noopener noreferrer"&gt;in this StackOverflow answer&lt;/a&gt; to create a cURL command that will upload the given file to dropbox. You will only need to change &lt;code&gt;--data-binary @matrices.txt&lt;/code&gt; into &lt;code&gt;--data-binary @-&lt;/code&gt; so that the commands gets it input from stdin, that is the output of other &lt;code&gt;curl&lt;/code&gt; command you pipe into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The trick presented here is quite straightforward - thanks to Chrome's DevTools, you can easily convert any network request your browser makes into a cURL command you can run anywhere and in most cases will produce the same result. I leveraged this method in order to transfer large files using Linux commands on the cloud, but I am sure you can find even more creative use cases. When you do, please share them with me :-)&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>aws</category>
      <category>productivity</category>
    </item>
    <item>
      <title>My First Dev.To Post</title>
      <dc:creator>Uri Shaked</dc:creator>
      <pubDate>Mon, 03 Jun 2019 13:06:32 +0000</pubDate>
      <link>https://dev.to/urishaked/my-first-dev-to-post-36i4</link>
      <guid>https://dev.to/urishaked/my-first-dev-to-post-36i4</guid>
      <description>&lt;p&gt;What should my next post be about? 🤔&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
