{"id":2141,"date":"2021-02-08T15:28:33","date_gmt":"2021-02-08T12:28:33","guid":{"rendered":"https:\/\/artem.services\/?p=2141"},"modified":"2022-06-08T22:31:26","modified_gmt":"2022-06-08T19:31:26","slug":"python-aws-s3-%d1%85%d1%80%d0%b0%d0%bd%d0%b8%d1%82%d1%8c-n-%d0%bf%d0%be%d1%81%d0%bb%d0%b5%d0%b4%d0%bd%d0%b8%d1%85-%d0%b0%d1%80%d1%82%d0%b5%d1%84%d0%b0%d0%ba%d1%82%d0%be%d0%b2","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2141","title":{"rendered":"Python &#8212; AWS S3 \u0445\u0440\u0430\u043d\u0438\u0442\u044c N \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442\u043e\u0432"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter wp-image-2273 size-full\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png\" alt=\"\" width=\"1024\" height=\"500\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-300x146.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-768x375.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2021\/03\/Python-logo-954x466.png 954w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0435 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438, \u043a\u0440\u043e\u043c\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 &quot;<strong>N<\/strong>&quot; \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430:<\/p>\n<ul>\n<li>\u0418\u043c\u044f \u043a\u043e\u0440\u0437\u0438\u043d\u044b<\/li>\n<li>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 main.py artifacts-artem-services 3\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>main.py:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sys\r\nimport boto3\r\n\r\ndef cleanup():\r\n    get_last_modified = lambda obj: int(obj[&#039;LastModified&#039;].strftime(&#039;%s&#039;))\r\n\r\n    s3 = boto3.client(&#039;s3&#039;)\r\n    result = s3.list_objects(Bucket=bucket, Delimiter=&#039;\/&#039;)\r\n    for dir in result.get(&#039;CommonPrefixes&#039;):\r\n        print(&#039;Directory: &#039; + str(dir[&#039;Prefix&#039;]))\r\n        artifacts_listing = s3.list_objects_v2(Bucket = bucket, Prefix = dir.get(&#039;Prefix&#039;))[&#039;Contents&#039;]\r\n        artifacts_sorted = [obj[&#039;Key&#039;] for obj in sorted(artifacts_listing, key=get_last_modified)]\r\n        for artifact in artifacts_sorted[:-keep_last]:\r\n            print(&#039;Deleting artifact: &#039; + str(artifact))\r\n            s3.delete_object(Bucket = bucket, Key = artifact)\r\n\r\nif sys.argv[1:] and sys.argv[2:]:\r\n    bucket = sys.argv[1]\r\n    keep_last = int(sys.argv[2])\r\n    cleanup()\r\nelse:\r\n    print(&quot;This script for cleanup old artifacts in S3 bucket&quot;)\r\n    print(&quot;Usage  : python3 &quot; + sys.argv[0] + &quot; {BUCKET_NAME} &quot; + &quot;{NUMBER_OF_THE_LAST_KEEPING_ARTIFACTS}&quot;)\r\n    print(&quot;Example: python3 &quot; + sys.argv[0] + &quot; artifacts-artem-services &quot; + &quot;3&quot;)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0435 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438, \u043a\u0440\u043e\u043c\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 &quot;N&quot; \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430: \u0418\u043c\u044f \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430: &nbsp; main.py:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[25,1423,1159,1161,35],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2141"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2141"}],"version-history":[{"count":5,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2141\/revisions"}],"predecessor-version":[{"id":2287,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2141\/revisions\/2287"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}