<?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 Gregory Sánchez on Medium]]></title>
        <description><![CDATA[Stories by Gregory Sánchez on Medium]]></description>
        <link>https://medium.com/@gregsanz182?source=rss-d183ec29083e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6oGBeHz9bbmg9w1oBzT_vw.jpeg</url>
            <title>Stories by Gregory Sánchez on Medium</title>
            <link>https://medium.com/@gregsanz182?source=rss-d183ec29083e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 04:06:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gregsanz182/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[Upload files to AWS S3 with public read ACL using AWS CLI or Boto 3]]></title>
            <link>https://medium.com/plusteam/upload-files-to-aws-s3-with-public-read-acl-using-aws-cli-or-boto-3-2e2bc67bbd63?source=rss-d183ec29083e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e2bc67bbd63</guid>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[boto]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Gregory Sánchez]]></dc:creator>
            <pubDate>Wed, 19 May 2021 14:00:31 GMT</pubDate>
            <atom:updated>2021-05-19T14:00:31.570Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*x-c1d8MuWDpGVKjfgTjR7A.jpeg" /></figure><p>As you might notice, when you upload files to <strong>AWS S3,</strong> it stores the objects as private by default. This applies to both AWS CLI and Boto 3 tools when uploading files.</p><p>In some cases, you might need to store the given files as public read. For example, when you don’t want to provide a <a href="https://www.cloudberrylab.com/resources/blog/s3-pre-signed-url-guide/">presigned url</a> to access the resource.</p><p>To manage an object’s access permissions, AWS uses an <strong>Access Control List </strong>(AWS). This ACL stores all the users and groups that have access to read — or write — an object.</p><p>Assuming you want your files to be public readable, you have to give read access to the AWS <strong>AllUsers</strong> group. To do this, both AWS CLI and Boto 3, provide a tool to manage the ACL.</p><h4>Copy or upload files with a given ACL using AWS CLI</h4><p>To upload or copy files — using cp command — to a bucket grating public access, you have to specify the value <strong>public-read</strong> in the — acl flag:</p><pre><strong>aws</strong> <strong>s3</strong> <strong>cp</strong> church_image.jpg s3://bucket_name/tests/church_image.jpg   --<strong>acl</strong> public-read</pre><p>After that, we could use the next URL to access the object without giving the URL token access:</p><pre><strong>https://bucket_name.s3.amazonaws.com/</strong>tests/church_image.jpg</pre><p>Also, you can use this — acl flag alongside the <strong>mv</strong> command.</p><p><a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl">Here</a> you can find a list of a variety of grants that you can use with the — acl flag.</p><h4>Change an Object’s ACL that is already within a bucket using AWS CLI</h4><p>To change the permissions of an object that is already within a bucket, you can use the <strong>put-object-acl</strong> command of the <strong>s3api</strong> tool.</p><pre><strong>aws s3api put-object-acl --bucket</strong> bucket-name <strong>--key</strong> my_object <strong>--acl</strong> public-read</pre><h4>Upload files with a given ACL using Boto 3</h4><p>To upload a file with given permission you must specify the ACL using the <strong>ExtraArgs</strong> parameter within the <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_file">upload_file</a> or <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_fileobj">upload_fileobj</a> methods.</p><pre><strong>import</strong> boto3</pre><pre>s3_resource = boto3.resource(‘s3’)<br>s3_resource.meta.client.upload_file(<br>    ‘/tmp/church_image.jpg’,<br>    ‘bucket_name’,<br>    ‘tests/church_image.jpg’,<br>    ExtraArgs={‘ACL’: ‘public-read’})</pre><p>There is a variety of defined grants. You can find them <a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl">here</a>.</p><p>After that, we could use the next URL to access the object without giving the URL an access token:</p><pre><strong>https://bucket_name.s3.amazonaws.com/</strong>tests/church_image.jpg</pre><h4>Check an objects ACL</h4><p>Of course, you can use AWS CLI or Boto 3 to check the Access Control List of an object.</p><p>The tool <strong>s3api</strong> allows you to check the ACL with the command <strong>get-object-acl</strong>:</p><pre><strong>aws s3api get-object-acl --bucket</strong> my-bucket --<strong>key</strong> index.html</pre><p>With Boto 3 you can get this ACL creating an <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#objectacl">ObjectACL</a></p><pre><strong>import</strong> boto3</pre><pre>s3_resource = boto3.resource(‘s3’)<br>s3_resource.ObjectAcl(‘bucket-name’, ‘tests/church_image.jpg’)</pre><p>Both <strong>s3api</strong> and <strong>ObjectACL</strong> will return a dictionary with the ACL. The part of our interest here is the “Grants” key. Here you can find the groups and users that have access to the object, and the type of access that is granted.</p><p>When the object is public read, a group with the <strong>http://acs.amazonaws.com/groups/global/AllUsers</strong> and the “<strong>READ</strong>” permission will be found within this section.</p><pre>{<br>  &quot;Owner&quot;: { <br>    &quot;DisplayName&quot;: &quot;the_user&quot;, <br>    &quot;ID&quot;: &quot;FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF”<br>  }, <br>  “Grants”: [<br>    {<br>      &quot;Grantee&quot;: {<br>        &quot;DisplayName&quot;: &quot;the_user&quot;, <br>        &quot;ID&quot;: “FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF”, <br>        &quot;Type&quot;: &quot;CanonicalUser&quot;<br>      },<br>      &quot;Permission&quot;: &quot;FULL_CONTROL&quot;<br>    },<br>    {<br>      &quot;Grantee”: {<br>        &quot;Type&quot;: &quot;Group&quot;,<br>        &quot;URI&quot;: &quot;<a href="http://acs.amazonaws.com/groups/global/AllUsers">http://acs.amazonaws.com/groups/global/AllUsers</a>&quot;<br>      }, <br>      &quot;Permission&quot;: &quot;READ&quot;<br>    } <br>  ]<br>}</pre><p>No matter what are the permissions you want to grant to a user, you can use the ACL to give restricted access to an object, depending on what you may want.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e2bc67bbd63" width="1" height="1" alt=""><hr><p><a href="https://medium.com/plusteam/upload-files-to-aws-s3-with-public-read-acl-using-aws-cli-or-boto-3-2e2bc67bbd63">Upload files to AWS S3 with public read ACL using AWS CLI or Boto 3</a> was originally published in <a href="https://medium.com/plusteam">plusteam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Saleor and AWS: Files with custom ACL and how to disable files overwriting]]></title>
            <link>https://medium.com/plusteam/saleor-and-aws-files-with-custom-acl-and-how-to-disable-files-overwriting-254b2c30eefc?source=rss-d183ec29083e------2</link>
            <guid isPermaLink="false">https://medium.com/p/254b2c30eefc</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[s3-bucket]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[saleor]]></category>
            <category><![CDATA[aws-s3]]></category>
            <dc:creator><![CDATA[Gregory Sánchez]]></dc:creator>
            <pubDate>Tue, 25 Jun 2019 15:02:10 GMT</pubDate>
            <atom:updated>2019-06-25T15:02:10.136Z</atom:updated>
            <content:encoded><![CDATA[<h3>Saleor and AWS S3: Files with custom ACL and how to disable files overwriting</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*mTDGAYXROnxWMz56Nyw2DA.jpeg" /></figure><p>As you might know, <a href="https://getsaleor.com/">Saleor</a> is an open source storefront, that features out-of-the-box solutions to manage an e-commerce platform.</p><p>Saleor is developed with Django, and a lot of projects use it as a base to build custom market places. This has led to the fork of the <a href="https://github.com/mirumee/saleor">main project</a> and to a great number of developers to experiment with it.</p><p>One of the cool features about Saleor is the storing of files within <a href="https://aws.amazon.com/es/s3/">AWS S3</a> buckets. To do this, Saleor uses <a href="https://django-storages.readthedocs.io/en/latest/">Django Storages</a>, a custom collection of storages for Django. Once you <a href="https://docs.getsaleor.com/en/latest/deployment/s3.html?highlight=AWS">set the credentials</a>, Saleor will allow us to use a bucket as media storage.</p><p>Although <strong>Storages</strong> take care of uploading the files, you can face some scenarios where you might need more advanced solutions. These are two of those possible cases.</p><h4>Avoid file overwriting when uploading to a bucket</h4><p>By default, if an uploading file has the same name and path as an existing file, Saleor will overwrite that file. If you want to avoid this behavior and keep both files, you must add this setting to Saleor’s <em>settings.py</em> file:</p><pre># In saleor/settings.py</pre><pre>AWS_S3_FILE_OVERWRITE = os.environ.get(&#39;AWS_S3_FILE_OVERWRITE&#39;, False)</pre><p>This setting tells the <strong>Storage</strong> to add a random suffix to the file if exists a file with the same path and name in the bucket.</p><p>You should notice the use of an <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=15&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwit9NG164LjAhWKrVkKHUISCO8QFjAOegQICBAB&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEnvironment_variable&amp;usg=AOvVaw34sCMWmzJwwi3Y9VXbNPA1">environment variable</a>. If the env variable is not set, then the default value would be <strong>False</strong>, so the files won’t be overwritten.</p><h4>Change AWS Storage to manage different ACL depending on the FileField</h4><p>Saleor uses Django’s <a href="https://docs.djangoproject.com/es/2.1/ref/models/fields/#filefield">FileField</a> to store files in the configured storage.</p><p>You can set the object’s <strong>ACL</strong> via the <em>AWS_DEFAULT_ACL</em> env variable. But, this configuration will affect <strong>ALL</strong> of the uploaded files. This can be an issue if you want to manage different ACLs depending on the type of resource you want to store.</p><p>For example, let’s assume you’ve set the <em>AWS_DEFAULT_ACL</em> variable to <em>public-read</em>. But you wish to have some <strong>FileFields</strong> that store files with private access.</p><p>To solve this, you can create a new <strong>Storage</strong> extending the <strong>S3MediaStorage</strong> (<em>saleor/core/storages.py</em>) class. That class is the one responsible for uploading the files to AWS. The idea is to write this custom behavior in a child class. You can place this new Storage within the same Saleor application tree, or in a new module, if you wish.</p><pre># In saleor/core/storages.<br><strong>from</strong> django.conf <strong>import</strong> settings<br><strong>from</strong> storages.backends.s3boto3 <strong>import</strong> S3Boto3Storage</pre><pre><strong>class</strong> S3MediaStorage(S3Boto3Storage):<br> <strong>def</strong> __init__(self, *args, **kwargs):<br>   <strong>self</strong>.bucket_name = settings.AWS_MEDIA_BUCKET_NAME<br>   <strong>self</strong>.custom_domain = settings.AWS_MEDIA_CUSTOM_DOMAIN<br>   <strong>super</strong>().__init__(*args, **kwargs)</pre><pre># New class to manage private storage<br><strong>class</strong> PrivateMediaStorage(S3MediaStorage):<br> <strong>def</strong> __init__(self, *args, **kwargs):<br>   <strong>super</strong>().__init__(*args, **kwargs)<br>   <strong>self</strong>.default_acl = &#39;private&#39;</pre><p>As you may notice, <strong>S3MediaStorage</strong> inherits <strong>S3Boto3Storage</strong>. This is the storage provided by the Django Storages collection that allows us to use AWS S3.</p><p>For our specific purposes, we only have to create a new class extending <strong>S3MediaStorage</strong>. This way we will keep all the features already available by this storage, and overwrite the ACL value that we are going to use.</p><p>Then later, when you create some model to use this feature, you only need to specify the new Storage on the FileField.</p><pre><strong>from</strong> saleor.core.storages <strong>import</strong> PrivateMediaStorage</pre><pre><strong>class</strong> Revision(models.Model):<br> created_at = models.DateTimeField(default=now, blank=True)<br> file = models.FileField(<br>    blank=True, max_length=250, storage=PrivateMediaStorage())</pre><p>And that’s it. Our <strong>FileField</strong> will store files with private access.</p><p>You have to notice that you won’t be able to store files locally if you don’t set the AWS credentials. This is because we are overriding the default storage.</p><p>If you want to keep this compatibility, you can create a function that returns the storage type depending on the state of the AWS credentials.</p><pre># In saleor/core/utils.py<br><strong>from</strong> saleor.core.storages <strong>import</strong> PrivateMediaStorage<br><strong>from</strong> saleor.settings <strong>import</strong> AWS_MEDIA_BUCKET_NAME</pre><pre><strong>def</strong> get_private_storage():<br> # Si el bucket está definido<br> <strong>if</strong> AWS_MEDIA_BUCKET_NAME:<br>   <strong>return</strong> PrivateMediaStorage()<br>   <strong>return</strong> None</pre><pre># In saleor/core/storages.py<br><strong>from</strong> saleor.core.utils <strong>import</strong> get_private_storage</pre><pre><strong>class</strong> Revision(models.Model):<br> created_at = models.DateTimeField(default=now, blank=True)<br> file = models.FileField(<br>    blank=True, max_length=250,<br>    storage=get_private_storage())</pre><p>Now, the function will detect if AWS Storage is enabled. Then, the <strong>FileField</strong> will use the custom Storage or the Default Storage of Django depending on the configured storage.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=254b2c30eefc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/plusteam/saleor-and-aws-files-with-custom-acl-and-how-to-disable-files-overwriting-254b2c30eefc">Saleor and AWS: Files with custom ACL and how to disable files overwriting</a> was originally published in <a href="https://medium.com/plusteam">plusteam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Move and Rename objects within an S3 Bucket using Boto 3]]></title>
            <link>https://medium.com/plusteam/move-and-rename-objects-within-an-s3-bucket-using-boto-3-58b164790b78?source=rss-d183ec29083e------2</link>
            <guid isPermaLink="false">https://medium.com/p/58b164790b78</guid>
            <category><![CDATA[s3-bucket]]></category>
            <category><![CDATA[boto]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Gregory Sánchez]]></dc:creator>
            <pubDate>Tue, 25 Jun 2019 14:44:30 GMT</pubDate>
            <atom:updated>2019-06-25T14:44:30.445Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*h_yBsxp-10hAbyn5Gr1V8g.jpeg" /></figure><p>Let’s suppose you are building an app that manages the files that you have on an AWS bucket. You decided to go with Python 3 and use the popular Boto 3 library, which in fact is the library used by <a href="https://aws.amazon.com/es/cli/">AWS CLI</a>.</p><p>AWS CLI provides a <a href="https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html">command to move objects</a>, so you hoped you could use this feature as well. But, to your surprise, you did not find any reference to any method which can do this operation using Boto 3. So, how does AWS CLI move and rename objects using this module?</p><p>First of all, you have to remember that <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=3&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiF-paW-oLjAhVD1lkKHfTCB3oQFjACegQIAhAB&amp;url=https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fdev%2FUsingBucket.html&amp;usg=AOvVaw2BpQ0qfyfTC9pGcqXZ55Ap">S3 buckets</a> do NOT have any “move” or “rename” operation. All you can do is create, copy and delete.</p><p>Under the hood, <strong>AWS CLI</strong> copies the objects to the target folder and then removes the original file. The same applies to the rename operation. So, simple enough, you can do the same in your own implementation using Boto 3.</p><p>If you want to move a file — or rename it — with Boto, you have to:</p><ol><li>Copy the object <strong>A</strong> to a new location within the same bucket. Thus, creating the object B.</li><li>Delete the former object A.</li></ol><p>Because there is no “move” o “rename” action in S3, we can simulate those actions by using the above steps. Pretty simple, eh?</p><p>So, if you wish to move an object, you can use this as an example (in Python 3):</p><pre><strong>import</strong> boto3</pre><pre>s3_resource = boto3.resource(‘s3’)</pre><pre># Copy object A as object B<br>s3_resource.Object(“bucket_name”, “newpath/to/object_B.txt”).copy_from(<br> CopySource=”path/to/your/object_A.txt”)</pre><pre># Delete the former object A<br>s3_resource.Object(“bucket_name”, “path/to/your/object_A.txt”).delete()</pre><p>This process works to <strong>rename objects</strong> as well.</p><h4>Moving files and grant public read access</h4><p>You can move — or rename — an object granting public read access through the ACL (Access Control List) of the new object. To do this, you have to pass the ACL to the <strong>copy_from</strong> method</p><pre>s3_resource.Object(“bucket_name”,<br>  “newpath/to/object_A.txt”).copy_from(<br>    CopySource=”path/to/your/object_B.txt”, ACL=’public-read’)</pre><p>This way, you will move or rename the object and every user will get access to the object and read it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58b164790b78" width="1" height="1" alt=""><hr><p><a href="https://medium.com/plusteam/move-and-rename-objects-within-an-s3-bucket-using-boto-3-58b164790b78">Move and Rename objects within an S3 Bucket using Boto 3</a> was originally published in <a href="https://medium.com/plusteam">plusteam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>