|
| 1 | +# S3 (Simple Storage Service) |
| 2 | +## Good to Know... |
| 3 | +S3 is more a key / value storage where the value is the content of a file. |
| 4 | +There's no such concept as folders in S3. But this is emulated by using prefixes |
| 5 | +containing `/` as folder separator in the key name. |
| 6 | + |
| 7 | +## Deleting an object |
| 8 | +With `aws s3` |
| 9 | + |
| 10 | +```shell |
| 11 | +aws s3 rm s3://bucket_name/object_key |
| 12 | +``` |
| 13 | + |
| 14 | +With `aws s3api` |
| 15 | + |
| 16 | +```shell |
| 17 | +aws s3api delete-object --bucket bucket_name --key object_key |
| 18 | +``` |
| 19 | + |
| 20 | +## Listing all older versions of all objects |
| 21 | +It is highly recommended to save the output in a file as it can be huge and long |
| 22 | +to obtain on large buckets. |
| 23 | + |
| 24 | +```shell |
| 25 | +aws s3api list-object-versions \ |
| 26 | + --bucket bucket_name \ |
| 27 | + | jq '[.Versions[] | select(.IsLatest == false)]' \ |
| 28 | + | jq -c ".[] | {Key, VersionId}" > bucket_older_versions.json |
| 29 | +``` |
| 30 | + |
| 31 | +### Filtering on a specific prefix |
| 32 | + |
| 33 | +Use option `--prefix`: |
| 34 | + |
| 35 | +```shell |
| 36 | +aws s3api list-object-versions \ |
| 37 | + --bucket bucket_name \ |
| 38 | + --prefix 'prefix/' \ |
| 39 | + | jq '[.Versions[] | select(.IsLatest == false)]' \ |
| 40 | + | jq -c ".[] | {Key, VersionId}" > bucket_older_versions.json |
| 41 | +``` |
| 42 | + |
| 43 | +### Delete all older versions of all objects |
| 44 | +Assuming we ran the above commands and saved the output in a file named |
| 45 | +`bucket_older_versions.json`, and using 20 threads: |
| 46 | + |
| 47 | +```shell |
| 48 | +cat bucket_older_versions.json | parallel -j 20 --linebuffer ' |
| 49 | + key=$(echo {} | jq -r ".Key") |
| 50 | + versionId=$(echo {} | jq -r ".VersionId") |
| 51 | + aws s3api delete-object --bucket bucket_name --key "$key" --version-id "$versionId" |
| 52 | +' |
| 53 | +``` |
| 54 | + |
| 55 | +## List all objects from only the root folder of a bucket |
| 56 | +```bash |
| 57 | +aws s3api list-object-v2 \ |
| 58 | + --bucket bucket_name \ |
| 59 | + --delimiter '/' \ |
| 60 | + --prefix '' |
| 61 | +``` |
| 62 | + |
| 63 | +## Computing size of objects in a bucket |
| 64 | +First generate a full listing of all objects in a bucket and save it in a file. |
| 65 | +```shell |
| 66 | +aws s3 ls --recursive s3://bucket_name > bucket_name_listing.txt |
| 67 | +``` |
| 68 | + |
| 69 | +The advantage of pregenerating the listing is that it can be used multiple times |
| 70 | +without having to query the S3 API (and get charged) again and again. It is also |
| 71 | +much faster to process a local file. |
| 72 | + |
| 73 | +The downside is the initial time to generate that listing and the fact it may |
| 74 | +not be up to date. So use that approach mostly for very large buckets. |
| 75 | + |
| 76 | +### Total size of all objects of the bucket |
| 77 | +```shell |
| 78 | +awk '{sum+=$3} END {print sum}' bucket_name_listing.txt |
| 79 | +``` |
| 80 | + |
| 81 | +To get the size in MB, divide by (1024 * 1024). For sizes in GB, divide by |
| 82 | +(1024 * 1024 * 1024). |
| 83 | + |
| 84 | +Total size of all objects in GB: |
| 85 | + |
| 86 | +```shell |
| 87 | +awk '{sum+=$3} END {print sum/(1024*1024*1024)}' bucket_name_listing.txt |
| 88 | +``` |
| 89 | + |
| 90 | +### Total Size of Objects in a Specific Folder In GB |
| 91 | +```shell |
| 92 | +grep 'folder_name/' bucket_name_listing.txt \ |
| 93 | + | awk '{sum+=$3} END {print sum/(1024*1024*1024)}' |
| 94 | +``` |
| 95 | + |
| 96 | +## Exploring a Bucket's Access Log. |
| 97 | +If a `bucket_name` bucket has access logs enabled, and the logs are stored at |
| 98 | +`s3://bucket_logs/bucket_name/`, then it is possible to query them with an |
| 99 | +**Athena table**. |
| 100 | + |
| 101 | +Create the table with the following SQL |
| 102 | +```sql |
| 103 | +CREATE EXTERNAL TABLE bucket_name_access_logs ( |
| 104 | + bucketowner STRING, |
| 105 | + bucket_name STRING, |
| 106 | + requestdatetime STRING, |
| 107 | + remoteip STRING, |
| 108 | + requester STRING, |
| 109 | + requestid STRING, |
| 110 | + operation STRING, |
| 111 | + key STRING, |
| 112 | + request_uri STRING, |
| 113 | + httpstatus STRING, |
| 114 | + errorcode STRING, |
| 115 | + bytessent BIGINT, |
| 116 | + objectsize BIGINT, |
| 117 | + totaltime STRING, |
| 118 | + turnaroundtime STRING, |
| 119 | + referrer STRING, |
| 120 | + useragent STRING, |
| 121 | + versionid STRING, |
| 122 | + hostid STRING, |
| 123 | + sigv STRING, |
| 124 | + ciphersuite STRING, |
| 125 | + authtype STRING, |
| 126 | + endpoint STRING, |
| 127 | + tlsversion STRING, |
| 128 | + accesspointarn STRING, |
| 129 | + aclrequired STRING) |
| 130 | +ROW FORMAT SERDE |
| 131 | + 'org.apache.hadoop.hive.serde2.RegexSerDe' |
| 132 | +WITH SERDEPROPERTIES ( |
| 133 | + 'input.regex'='([^ ]*) ([^ ]*) \\[(.*?)\\] ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (\"[^\"]*\"|-) (-|[0-9]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (\"[^\"]*\"|-) ([^ ]*)(?: ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*))?.*$') |
| 134 | +STORED AS INPUTFORMAT |
| 135 | + 'org.apache.hadoop.mapred.TextInputFormat' |
| 136 | +OUTPUTFORMAT |
| 137 | + 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' |
| 138 | +LOCATION |
| 139 | + 's3://bucket_logs/bucket_name/' |
| 140 | + ``` |
| 141 | + |
| 142 | +Then looking for logs is straightforward, but there's a little caveat. The |
| 143 | +`requestdatetime` field is not in a `datetime` friendly format. To convert it, |
| 144 | +use: |
| 145 | + |
| 146 | +```sql |
| 147 | +parse_datetime(requestdatetime, 'dd/MMM/yyyy:HH:mm:ss Z') |
| 148 | +``` |
| 149 | + |
| 150 | +For example, to get the most recent GET requests on the bucket: |
| 151 | + |
| 152 | +```sql |
| 153 | +SELECT |
| 154 | + * |
| 155 | +FROM |
| 156 | + bucket_name_access_logs |
| 157 | +WHERE |
| 158 | + operation LIKE 'REST.GET.%' |
| 159 | +ORDER BY |
| 160 | + parse_datetime(requestdatetime, 'dd/MMM/yyyy:HH:mm:ss Z') DESC |
| 161 | +``` |
0 commit comments