{"id":7854,"date":"2023-04-17T16:10:22","date_gmt":"2023-04-17T21:10:22","guid":{"rendered":"https:\/\/abudinen.com\/blog\/?p=7854"},"modified":"2023-04-19T10:20:06","modified_gmt":"2023-04-19T15:20:06","slug":"allow-public-read-access-to-an-aws-s3-bucket","status":"publish","type":"post","link":"https:\/\/abudinen.com\/blog\/2023\/04\/17\/allow-public-read-access-to-an-aws-s3-bucket\/","title":{"rendered":"Allow Public Read access to an AWS S3 Bucket"},"content":{"rendered":"\n#&nbsp;Allow Public Read access to an S3 Bucket\n\n\n\nTo allow public read access to an S3 bucket:\n\n\n\n\nOpen the AWS S3 console and click on the bucket&#8217;s name\n\n\n\nClick on the&nbsp;Permissions&nbsp;tab\n\n\n\nFind the&nbsp;Block public access (bucket settings)&nbsp;section, click on the&nbsp;Edit&nbsp;button, uncheck the checkboxes and click on&nbsp;Save changes\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nIn the&nbsp;Permissions&nbsp;tab, scroll down to the&nbsp;Bucket policy&nbsp;section and click on the&nbsp;Edit&nbsp;button. Paste the following policy into the textarea to grant public read access to all files in your S3 bucket.\n\n\n\n\nReplace the&nbsp;YOUR_BUCKET_NAME&nbsp;placeholder with your bucket&#8217;s name.\n\n\n\nbucket-policy-public-read\n\n\n\n{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": &#91;\n    {\n      \"Effect\": \"Allow\",\n      \"Principal\": \"*\",\n      \"Action\": &#91;\n        \"s3:GetObject\"\n      ],\n      \"Resource\": \"arn:aws:s3:::YOUR_BUCKET_NAME\/*\"\n    }\n  ]\n}\n\n\n\n\nFor example, the bucket policy of an&nbsp;S3 bucket&nbsp;with the name&nbsp;my-bucket&nbsp;will look like:\n\n\n\n\n\n\n\nSave the changes you&#8217;ve made to the bucket&#8217;s <span class=\"maquina-leer-mas\">[...x]<\/span><div id=\"premium-content-gate\" style=\"display:none;\" class=\"contenido-premium\">policy and your bucket will have public read access enabled.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>(Optional) &#8211; If you need to access your bucket with HTTP requests from the browser, you have to update the bucket&#8217;s&nbsp;Cross-origin resource sharing (CORS) options&nbsp;to allow your frontend&#8217;s requests\n<ul class=\"wp-block-list\">\n<li>In the&nbsp;<code>Permissions<\/code>&nbsp;tab of your S3 bucket, scroll down to the&nbsp;<code>Cross-origin resource sharing (CORS)<\/code>&nbsp;section and click on the&nbsp;<code>Edit<\/code>&nbsp;button<\/li>\n\n\n\n<li>Paste the following JSON into the textarea and save the changes<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>cors-configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n  {\n      \"AllowedHeaders\": &#91;\n          \"Authorization\",\n          \"Content-Length\"\n      ],\n      \"AllowedMethods\": &#91;\n          \"GET\"\n      ],\n      \"AllowedOrigins\": &#91;\n          \"*\"\n      ],\n      \"ExposeHeaders\": &#91;],\n      \"MaxAgeSeconds\": 3000\n  }\n]\n<\/code><\/pre>\n\n\n\n<p><strong>To test that your bucket has public read access enabled:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Click on the&nbsp;<code>Objects<\/code>&nbsp;tab in your S3 bucket.<\/li>\n\n\n\n<li>Click on the checkbox next to a file&#8217;s name.<\/li>\n\n\n\n<li>Click on the&nbsp;<code>Copy URL<\/code>&nbsp;button at the top and copy the public URL of the file.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Paste the URL in your browser and you should see the contents of the file (for HTML files or images).<\/li>\n<\/ol>\n\n\n\n<p>Note that you&#8217;ll see a red badge with the text&nbsp;<code>Publicly accessible<\/code>&nbsp;next to your bucket&#8217;s name.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><\/figure>\n\n\n\n<p>In this case, the&nbsp;bucket policy&nbsp;only grants public read access to the bucket, so other people can&#8217;t add objects to your S3 bucket. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Either you can take query string inside Lambda like below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var result =  event&#91;\"queryStringParameters\"]&#91;'queryStringParam1']\n<\/code><\/pre>\n\n\n\n<p>According to your API URL,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var postcode =  event&#91;\"queryStringParameters\"]&#91;'postcode']\nvar house =  event&#91;\"queryStringParameters\"]&#91;'house']\n<\/code><\/pre>\n\n\n\n<p>or you can use body mapping template in the integration request section and get request body and query strings. Construct a new JSON at body mapping template, which will have data from request body and query string. As we are adding body mapping template your business logic will get the JSON we have constructed at body mapping template.<\/p>\n\n\n\n<p>Inside body mapping template to get query string please do ,<\/p>\n\n\n\n<p>$input.params(&#8216;querystringkey&#8217;)<\/p>\n\n\n\n<p>For example inside body mapping template,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#set($inputRoot = $input.path('$'))\n{\n\"firstName\" : \"$input.path('$.firstName')\",\n\"lastName\" : \"$input.path('$.lastName')\"\n\"language\" : \"$input.params('$.language')\"\n}<\/code><\/pre>\n\n\n\n<p>&nbsp;Run code snippet<\/p>\n\n\n\n<p>Expand snippet<\/p>\n\n\n\n<p>Please read&nbsp; more details on body mapping template<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">#&nbsp;Additional Resources<\/h2>\n\n\n\n<p>You can learn more about the related topics by checking out the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>List all Files in an S3 Bucket with AWS CLI<\/li>\n\n\n\n<li>Get the Size of a Folder in AWS S3 Bucket<\/li>\n\n\n\n<li>How to Get the Size of an AWS S3 Bucket<\/li>\n\n\n\n<li>Configure CORS for an AWS S3 Bucket<\/li>\n\n\n\n<li>Copy a Local Folder to an S3 Bucket<\/li>\n\n\n\n<li>Download a Folder from AWS S3<\/li>\n\n\n\n<li>How to Rename a Folder in AWS S3<\/li>\n\n\n\n<li>Copy Files and Folders between S3 Buckets<\/li>\n\n\n\n<li>How to Delete a Folder from an S3 Bucket<\/li>\n\n\n\n<li>Count Number of Objects in S3 Bucket<\/li>\n\n\n\n<li>Download an Entire S3 Bucket &#8211; Complete Guide<\/li>\n\n\n\n<li>AWS CDK Tutorial for Beginners &#8211; Step-by-Step Guide<\/li>\n\n\n\n<li>How to use Parameters in AWS CDK<\/li>\n\n\n\n<li>The Bucket you are attempting to Access AWS S3 Error [Fixed]<\/li>\n\n\n\n<li>How to Exclude multiple Folders with AWS S3 Sync<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests\nimport json\nimport time\n\ndef lambda_handler(event, context):\n    # TODO implement\n    url = \"\"\n    \n    print(event, context)\n    \n    httpMethod = event.get('httpMethod')\n    path = event.get('path')\n    if httpMethod == \"GET\" and path == \"\/Wh2\":\n        params = event.get('queryStringParameters')\n        if params is None:\n            print(params, 86666666666666)\n            text = (\"Hello there! My name is ' joi ', your English coach.\" \n            \"I'm really happy to start this journey with you. Let's get started by telling me your name and where you're from.\" \n            \"I'd love to learn more about you! And if you ever feel confused or need help, don't hesitate to ask me.\")\n        else:\n            text = params[\"text\"]\n            \n    print(text)\n            \n    \n    \n    #try:\n        #text = event[\"queryStringParameters\"][\"text\"]\n    #except KeyError as e:\n        #text = (\"Hello there! My name is ' joi ', your English coach.\" \n        #\"I'm really happy to start this journey with you. Let's get started by telling me your name and where you're from.\" \n        #\"I'd love to learn more about you! And if you ever feel confused or need help, don't hesitate to ask me.\")\n    \n    payload = json.dumps({\n      \"voice\": \"en-US-DavisNeural\",\n      \"content\": [text],\n       \"title\": \"Testing public api convertion\"\n    })\n    headers = {\n      #'Authorization': 'f592b758e0ee4094a4fad34be3371663',\n      'Authorization': '86b294b3b5474335ab5e2a49f7b956c9',\n      #'X-User-ID': 'zoSFLZ0CUsajZj4NliirGr1qgt73',\n      'X-User-ID': '8biOMUQv0IXAxYRdj1TQJmYUmwD3',\n      'Content-Type': 'application\/json'\n    }\n    \n    response = requests.request(\"POST\", url, headers=headers, data=payload)\n    print(response.text)\n    data = json.loads(response.text)\n    print(data['transcriptionId'])\n    \n    time.sleep(2)\n    url = ''+data['transcriptionId']\n    x = requests.get(url, headers=headers)\n    data = json.loads(x.text)\n    print(data['audioUrl'])\n    \n    return {\n        'statusCode': 200,\n        'body': json.dumps(data['audioUrl'])\n    }<\/pre>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>#&nbsp;Allow Public Read access to an S3 Bucket To allow public read access to an S3 bucket: Replace the&nbsp;YOUR_BUCKET_NAME&nbsp;placeholder with your bucket&#8217;s name. bucket-policy-public-read For example, the bucket policy of an&nbsp;S3 bucket&nbsp;with the name&nbsp;my-bucket&nbsp;will look like: Save the changes you&#8217;ve made to the bucket&#8217;s policy and your bucket will have public read access enabled. cors-configuration &#8230; <a title=\"Allow Public Read access to an AWS S3 Bucket\" class=\"read-more\" href=\"https:\/\/abudinen.com\/blog\/2023\/04\/17\/allow-public-read-access-to-an-aws-s3-bucket\/\" aria-label=\"Read more about Allow Public Read access to an AWS S3 Bucket\">Leer m\u00e1s<\/a><\/p>\n\n        <p class=\"social-share\">\n            <strong><span>Sharing is caring<\/span><\/strong> <!--<i class=\"fa fa-share-alt\"><\/i>&nbsp;&nbsp;-->\n            <a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fabudinen.com%2Fblog%2F2023%2F04%2F17%2Fallow-public-read-access-to-an-aws-s3-bucket%2F\" target=\"_blank\" class=\"facebook\"><i class=\"fab fa-facebook\"><\/i> <span>Share<\/span><\/a>\n            <a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fabudinen.com%2Fblog%2F2023%2F04%2F17%2Fallow-public-read-access-to-an-aws-s3-bucket%2F\" target=\"_blank\" class=\"gplus\"><i class=\"fab fa-google-plus\"><\/i> <span>+1<\/span><\/a>\n            <a href=\"https:\/\/twitter.com\/intent\/tweet?text=Allow%20Public%20Read%20access%20to%20an%20AWS%20S3%20Bucket&amp;url=https%3A%2F%2Fabudinen.com%2Fblog%2F2023%2F04%2F17%2Fallow-public-read-access-to-an-aws-s3-bucket%2F&amp;via=YOUR_TWITTER_HANDLE_HERE\" target=\"_blank\" class=\"twitter\"><i class=\"fab fa-twitter\"><\/i> <span>Tweet<\/span><\/a>\n            <a href=\"http:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=Allow%20Public%20Read%20access%20to%20an%20AWS%20S3%20Bucket\" target=\"_blank\" class=\"linkedin\"><i class=\"fab fa-linkedin\"><\/i> <span>Share<\/span><\/a>\n            <a href=\"https:\/\/wa.me\/?text=Allow%20Public%20Read%20access%20to%20an%20AWS%20S3%20Bucket https%3A%2F%2Fabudinen.com%2Fblog%2F2023%2F04%2F17%2Fallow-public-read-access-to-an-aws-s3-bucket%2F\" target=\"_blank\" class=\"whatsapp\"><i class=\"fab fa-whatsapp\"><\/i> <span>Share<\/span><\/a>\n            <w>965 words 149 views<\/w>\n        <\/p>","protected":false},"author":1,"featured_media":7859,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sin-categoria"],"_links":{"self":[{"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/posts\/7854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/comments?post=7854"}],"version-history":[{"count":4,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/posts\/7854\/revisions"}],"predecessor-version":[{"id":7863,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/posts\/7854\/revisions\/7863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/media\/7859"}],"wp:attachment":[{"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/media?parent=7854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/categories?post=7854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abudinen.com\/blog\/wp-json\/wp\/v2\/tags?post=7854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}