{"id":197,"date":"2015-10-15T11:28:48","date_gmt":"2015-10-15T10:28:48","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=197"},"modified":"2015-10-15T11:31:59","modified_gmt":"2015-10-15T10:31:59","slug":"cloud-files-container-bulk-deletion-script-written-in-bash","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2015\/10\/15\/cloud-files-container-bulk-deletion-script-written-in-bash\/","title":{"rendered":"Cloud Files Container Bulk Deletion Script written in BASH"},"content":{"rendered":"<p>So, we had a lot of customers asking for ways to delete all of their cloud files in a single container, instead of having to do manually. This is possible using the bulk delete function defined in the rackspace docs. Find below the steps required to do this.<\/p>\n<p>Step 1: Make an auth.json file (for simplicity)<\/p>\n<pre>\r\n{\r\n    \"auth\": {\r\n        \"RAX-KSKEY:apiKeyCredentials\": {\r\n            \"username\": \"mycloudusername\",\r\n            \"apiKey\": \"mycloudapikey\"\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>It&#8217;s quite simple and nothing intimidating.<\/p>\n<p><strong> For step 2 I&#8217;m using an application called jq, to install it do<\/p>\n<pre>\r\n\r\nwget https:\/\/github.com\/stedolan\/jq\/releases\/download\/jq-1.5\/jq-linux64\r\nmv jq-linux64 \/bin\/jq\r\nalias jq = '\/bin\/jq'\r\n<\/pre>\n<p>Now you can use jq at the commandline.<\/p>\n<p>Step 2: Set variable called $TOKEN that can store the api token password output, the nice thing is there is no token stored in the script so its kind of secure<\/p>\n<pre>\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d @auth.json -H \"Content-type: application\/json\" | jq .access.token.id | sed 's\/\"\/\/g'`\r\necho $TOKEN\r\n<\/pre>\n<p>Step 3: Set a variable for the container name<\/p>\n<pre>\r\n# Container to Upload to\r\nCONTAINER=meh2\r\n<\/pre>\n<p>Step 4: Populate a List of all the files in the $CONTAINER variable, in this case &#8216;meh2&#8217;. <\/p>\n<pre>\r\n# Populate File List\r\necho \"Populating File List\"\r\ncurl -X GET https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\/meh2 -H \"X-Auth-Token: $TOKEN\" > filelist.txt\r\n<\/pre>\n<p>Step 5: Add container name to the file listing by rewriting the output file filelist.txt to a deletelist.txt<\/p>\n<pre>\r\nsed -e \"s\/^\/\\\/$CONTAINER\\\/\/\" <  filelist.txt > deletelist.txt\r\n<\/pre>\n<p>Step 6: Bulk Delete Files thru API<\/p>\n<pre>\r\necho \"Deleting Files..\"\r\ncurl -i -v -XDELETE -H\"x-auth-token: $TOKEN\" https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\\?bulk-delete -T .\/deletelist.txt\r\n<\/pre>\n<p>Step 7: Confirm the deletion success<\/p>\n<pre>\r\n# Confirm Deleted\r\necho \"Confirming Deleted in $CONTAINER..\"\r\ncurl -i -X GET https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\/meh2 -H \"X-Auth-Token: $TOKEN\"\r\n<\/pre>\n<p>The completed script looks like this:<\/p>\n<pre>\r\n\r\n Mass Delete Container\r\n\r\n# Get Token\r\n\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d @auth.json -H \"Content-type: application\/json\" | jq .access.token.id | sed 's\/\"\/\/g'`\r\necho $TOKEN\r\n\r\n# Container to Upload to\r\nCONTAINER=meh2\r\n\r\n\r\n# Populate File List\r\necho \"Populating File List\"\r\ncurl -X GET https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\/meh2 -H \"X-Auth-Token: $TOKEN\" > filelist.txt\r\n\r\n\r\n# Add Container Prefix\r\necho \"Adding Container Prefix..\"\r\nsed -e \"s\/^\/\\\/$CONTAINER\\\/\/\" <  filelist.txt > deletelist.txt\r\n\r\n\r\n# Delete Files\r\necho \"Deleting Files..\"\r\ncurl -i -v -XDELETE -H\"x-auth-token: $TOKEN\" https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\\?bulk-delete -T .\/deletelist.txt\r\n\r\n# Confirm Deleted\r\necho \"Confirming Deleted in $CONTAINER..\"\r\ncurl -i -X GET https:\/\/storage101.lon3.clouddrive.com\/v1\/MossoCloudFS_10045567\/meh2 -H \"X-Auth-Token: $TOKEN\"\r\n\r\n<\/pre>\n<p>Pretty simple!<\/p>\n<p>Running it.. <\/p>\n<pre>\n<p>* About to connect() to storage101.lon3.clouddrive.com port 443 (#0)<br \/>\n*   Trying 2a00:1a48:7900::100...<br \/>\n* Connected to storage101.lon3.clouddrive.com (2a00:1a48:7900::100) port 443 (#0)<br \/>\n* Initializing NSS with certpath: sql:\/etc\/pki\/nssdb<br \/>\n*   CAfile: \/etc\/pki\/tls\/certs\/ca-bundle.crt<br \/>\n  CApath: none<br \/>\n* SSL connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA<br \/>\n* Server certificate:<br \/>\n* \tsubject: CN=storage101.lon3.clouddrive.com<br \/>\n* \tstart date: May 18 00:00:00 2015 GMT<br \/>\n* \texpire date: Nov 17 23:59:59 2016 GMT<br \/>\n* \tcommon name: storage101.lon3.clouddrive.com<br \/>\n* \tissuer: CN=thawte DV SSL CA - G2,OU=Domain Validated SSL,O=\"thawte, Inc.\",C=US<br \/>\n> DELETE \/v1\/MossoCloudFS_10045567?bulk-delete HTTP\/1.1<br \/>\n> User-Agent: curl\/7.29.0<br \/>\n> Host: storage101.lon3.clouddrive.com<br \/>\n> Accept: *\/*<br \/>\n> x-auth-token: AAA7uz-F91SDsaMOCKTOKEN-gOLeB5bbffh8GBGwAPl9F313Pcy4Xg_zP8jtgZolMOudXhsZh-nh9xjBbOfVssaSx_shBMqkxIEEgW1zt8xESJbZLIsvBTNzfVBlTitbUS4RarUOiXEw<br \/>\n> Content-Length: 515<br \/>\n> Expect: 100-continue<br \/>\n><br \/>\n< HTTP\/1.1 100 Continue\nHTTP\/1.1 100 Continue\n\n* We are completely uploaded and fine\n< HTTP\/1.1 200 OK\nHTTP\/1.1 200 OK\n< Content-Type: text\/plain\nContent-Type: text\/plain\n< X-Trans-Id: tx010194ea9a104443b89bb-00161f7f1dlon3\nX-Trans-Id: tx010194ea9a104443b89bb-001611f7f1dlon3\n< Date: Thu, 15 Oct 2015 10:25:35 GMT\nDate: Thu, 15 Oct 2015 10:25:35 GMT\n< Transfer-Encoding: chunked\nTransfer-Encoding: chunked\n\n<\nNumber Deleted: 44\nNumber Not Found: 0\nResponse Body:\nResponse Status: 200 OK\nErrors:\n* Connection #0 to host storage101.lon3.clouddrive.com left intact\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, we had a lot of customers asking for ways to delete all of their cloud files in a single container, instead of having to do manually. This is possible using the bulk delete function defined in the rackspace docs. &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2015\/10\/15\/cloud-files-container-bulk-deletion-script-written-in-bash\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,19,15,14,10,7],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-api","category-bash","category-cloud","category-cloud-files","category-filesystem","category-management-tools"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":3,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}