<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/"></div></div><div><a data-url="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/" data-counturl="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/" data-count="horizontal" data-text="An R Function for Generating Authenticated URLs to Private Web Sites Hosted on AWS S3" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://alexioannides.com/2016/09/19/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/"> R – When Localhost Isn't Enough</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img title="HMAC" alt="Alt" src="https://i2.wp.com/alexioannides.files.wordpress.com/2016/08/hmac.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Quite often I want to share simple (static) web pages with other colleagues or clients. For example, I may have written a report using <a title="R Markdown @ R Studio" target="_blank" href="http://rmarkdown.rstudio.com/">R Markdown</a> and rendered it to HTML. AWS S3 can easily host such a simple web page (e.g. see <a title="AWS S3 Static Web Page" target="_blank" href="http://docs.aws.amazon.com/gettingstarted/latest/swh/website-hosting-intro.html">here</a>), but it cannot, however, offer any authentication to prevent anyone from accessing potentially sensitive information.</p>
<p>Yegor Bugayenko has created an external service <a title="S3 Authentication Service" target="_blank" href="http://www.s3auth.com/">S3Auth.com</a> that stands in the way of any S3 hosted web site, but this is a little too much for my needs. All I want to achieve is to limit access to specific S3 resources that will be largely transient in nature. A viable and simple solution is to use ‘query string request authentication’ that is described in detail <a title="AWS documentation" target="_blank" href="http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth">here</a>. I must confess to not really understanding what was going on here, until I had dug around on the web to see what others have been up to.</p>
<p>This blog post describes a simple R function for generating authenticated and ephemeral URLs to private S3 resources (including web pages) that only the holders of the URL can access.</p>
<p><span></span></p>
<h1>Creating User Credentials for Read-Only Access to S3</h1>
<p>Before we can authenticate anyone, we need someone to authenticate. From the AWS Management Console create a new user, download their security credentials and then attach the <code>AmazonS3ReadOnlyAccess</code> policy to them. For more details on how to do this, refer to a <a title="Part 1" target="_blank" href="https://alexioannides.com/2016/08/16/building-a-data-science-platform-for-rd-part-1-setting-up-aws/">previous post</a>. Note, that you should <strong>not</strong> create passwords for them to access the AWS console.</p>
<h1>Loading a Static Web Page to AWS S3</h1>
<p>Do <strong>not</strong> be tempted to follow the S3 ‘Getting Started’ page on how to host a static web page and in doing so enable ‘Static Website Hosting’. We need our resources to remain private and we would also like to use HTTPS, which this option does not support. Instead, create a new bucket and upload a simple HTML file <a title="Part 1" target="_blank" href="https://alexioannides.com/2016/08/16/building-a-data-science-platform-for-rd-part-1-setting-up-aws/">as usual</a>. An example html file – e.g. <code>index.html</code> – could be,</p>
<pre>
&lt;!DOCTYPE html>
&lt;html>
  &lt;body>
    &lt;p>Hello, World!&lt;/p>
  &lt;/body>
&lt;/html>
</pre>
<h1>An R Function for Generating Authenticated URLs</h1>
<p>We can now use our new user’s Access Key ID and Secret Access Key to create a URL with a limited lifetime that enables access to <code>index.html</code>. Technically, we are making a HTTP GET request to the S3 REST API, with the authentication details sent as part of a query string. Creating this URL is a bit tricky – I have adapted the Python example (number 3) that is provided <a title="Python Auth Example" target="_blank" href="https://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html">here</a>, as an R function (that can be found in the Gist below) – <code>aws_query_string_auth_url(...)</code>. Here’s an example showing this R function in action:</p>
<pre>
path_to_file &lt;- &quot;index.html&quot;
bucket &lt;- &quot;my.s3.bucket&quot;
region &lt;- &quot;eu-west-1&quot;
aws_access_key_id &lt;- &quot;DWAAAAJL4KIEWJCV3R36&quot;
aws_secret_access_key &lt;- &quot;jH1pEfnQtKj6VZJOFDy+t253OZJWZLEo9gaEoFAY&quot;
lifetime_minutes &lt;- 1
aws_query_string_auth_url(path_to_file, bucket, region, aws_access_key_id, aws_secret_access_key, lifetime_minutes)
# &quot;https://s3-eu-west-1.amazonaws.com/my.s3.bucket/index.html?AWSAccessKeyId=DWAAAKIAJL4EWJCV3R36&amp;Expires=1471994487&amp;Signature=inZlnNHHswKmcPfTBiKhziRSwT4%3D&quot;
</pre>
<p>And here’s the code for it as inspired by the short code snippet <a title="Python Auth Example" target="_blank" href="https://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html">here</a>:</p>
<p></p>
<p>Note the dependencies on the <code>digest</code> and <code>base64enc</code> packages.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alexioannides.wordpress.com/282/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/alexioannides.wordpress.com/282/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/"></div></div><div><a data-url="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/" data-counturl="https://www.r-bloggers.com/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/" data-count="vertical" data-text="An R Function for Generating Authenticated URLs to Private Web Sites Hosted on AWS S3" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://alexioannides.com/2016/09/19/an-r-function-for-generating-authenticated-urls-to-private-web-sites-hosted-on-aws-s3/"> R – When Localhost Isn't Enough</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CN54JhmJjpA"></a></div>