Skip to content

Commit 57209f8

Browse files
Merge pull request #25 from commoncrawl/feat/s3-access-via-warcio1.8
feat: add direct remote access over s3 and https via warcio >= 1.8.0
2 parents 3c04be3 + 513a5b0 commit 57209f8

5 files changed

Lines changed: 161 additions & 10 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,21 @@ jobs:
5757
- name: make iterate
5858
run: make iterate
5959

60+
- name: make iterate-remote
61+
run: make iterate-remote
62+
6063
- name: make cdxj
6164
run: make cdxj
6265

66+
- name: make cdxj-remote-https
67+
run: make cdxj-remote-https
68+
6369
- name: make extract
6470
run: make extract
6571

72+
- name: make extract-remote-https
73+
run: make extract-remote-https
74+
6675
- name: make cdx_toolkit
6776
run: make cdx_toolkit
6877

Makefile

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
EOT_IA_WARC_HTTPS = https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/IA-000/warc/EOT24PRE-20240926172119-crawl804_EOT24PRE-20240926172119-00000.warc.gz
2+
EOT_IA_WARC_S3 = s3://eotarchive/crawl-data/EOT-2024/segments/IA-000/warc/EOT24PRE-20240926172119-crawl804_EOT24PRE-20240926172119-00000.warc.gz
3+
EOT_CC_WARC_HTTPS = https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/CC-000/warc/EOT-2024-REPACKAGE-CC-MAIN-2024-42-GOV-000000-001.warc.gz
4+
EOT_CC_WARC_S3 = s3://eotarchive/crawl-data/EOT-2024/segments/CC-000/warc/EOT-2024-REPACKAGE-CC-MAIN-2024-42-GOV-000000-001.warc.gz
5+
WHIRLWIND_WARC_HTTPS = https://raw.githubusercontent.com/commoncrawl/whirlwind-python/refs/heads/main/whirlwind.warc.gz
6+
17
venv:
28
@echo "making a venv in ~/venv/whirlwind"
39
mkdir -p ~/venv
@@ -22,19 +28,57 @@ iterate:
2228
python ./warcio-iterator.py whirlwind.warc.wat.gz
2329
@echo
2430

31+
iterate-remote:
32+
@echo "iterating over whirlwind.warc.gz from GitHub via HTTPS:"
33+
python ./warcio-iterator.py $(WHIRLWIND_WARC_HTTPS)
34+
2535
cdxj:
2636
@echo "creating *.cdxj index files from the local warcs"
2737
cdxj-indexer whirlwind.warc.gz > whirlwind.warc.cdxj
2838
cdxj-indexer --records conversion whirlwind.warc.wet.gz > whirlwind.warc.wet.cdxj
2939
cdxj-indexer whirlwind.warc.wat.gz > whirlwind.warc.wat.cdxj
3040

41+
cdxj-remote-https:
42+
@echo "indexing End-of-Term-2024 Internet Archive WARC over HTTPS (File size ~1GB, showing first 10 records):"
43+
cdxj-indexer $(EOT_IA_WARC_HTTPS) 2>/dev/null | head -n 10 | tee eot-ia.cdxj
44+
@test -s eot-ia.cdxj || { echo "ERROR: no records indexed from $(EOT_IA_WARC_HTTPS) -- check network connectivity"; exit 1; }
45+
@echo
46+
@echo "indexing End-of-Term-2024 Common Crawl repackage WARC over HTTPS (File size ~1GB, showing first 10 records):"
47+
cdxj-indexer $(EOT_CC_WARC_HTTPS) 2>/dev/null | head -n 10 | tee eot-cc.cdxj
48+
@test -s eot-cc.cdxj || { echo "ERROR: no records indexed from $(EOT_CC_WARC_HTTPS) -- check network connectivity"; exit 1; }
49+
50+
cdxj-remote-s3:
51+
@echo "!! this step requires authentication via S3 credentials (even though it is free)"
52+
@echo "indexing End-of-Term-2024 Internet Archive WARC over S3 (File size ~1GB, showing first 10 records):"
53+
cdxj-indexer $(EOT_IA_WARC_S3) 2>/dev/null | head -n 10 | tee eot-ia.cdxj
54+
@test -s eot-ia.cdxj || { echo "ERROR: no records indexed from $(EOT_IA_WARC_S3) -- check network connectivity and S3 credentials"; exit 1; }
55+
@echo
56+
@echo "indexing End-of-Term-2024 Common Crawl repackage WARC over S3 (File size ~1GB, showing first 10 records):"
57+
cdxj-indexer $(EOT_CC_WARC_S3) 2>/dev/null | head -n 10 | tee eot-cc.cdxj
58+
@test -s eot-cc.cdxj || { echo "ERROR: no records indexed from $(EOT_CC_WARC_S3) -- check network connectivity and S3 credentials"; exit 1; }
59+
3160
extract:
3261
@echo "creating extraction.* from local warcs, the offset numbers are from the cdxj index"
3362
warcio extract --payload whirlwind.warc.gz 1023 > extraction.html
3463
warcio extract --payload whirlwind.warc.wet.gz 466 > extraction.txt
3564
warcio extract --payload whirlwind.warc.wat.gz 443 > extraction.json
3665
@echo "hint: python -m json.tool extraction.json"
3766

67+
extract-remote-https:
68+
@echo "extracting hpxml.nrel.gov record from End-of-Term Internet Archive WARC over HTTPS (offset 50755):"
69+
warcio extract $(EOT_IA_WARC_HTTPS) 50755
70+
@echo
71+
@echo "extracting before-you-ship.18f.gov record from End-of-Term Common Crawl repackage WARC over HTTPS (offset 18595):"
72+
warcio extract $(EOT_CC_WARC_HTTPS) 18595
73+
74+
extract-remote-s3:
75+
@echo "!! this step requires authentication via S3 credentials (even though it is free)"
76+
@echo "extracting hpxml.nrel.gov record from End-of-Term Internet Archive WARC over S3 (offset 50755):"
77+
warcio extract $(EOT_IA_WARC_S3) 50755
78+
@echo
79+
@echo "extracting before-you-ship.18f.gov record from End-of-Term Common Crawl repackage WARC over S3 (offset 18595):"
80+
warcio extract $(EOT_CC_WARC_S3) 18595
81+
3882
cdx_toolkit:
3983
@echo demonstrate that we have this entry in the index
4084
cdxt --crawl CC-MAIN-2024-22 --from 20240518015810 --to 20240518015810 iter an.wikipedia.org/wiki/Escopete
@@ -57,7 +101,7 @@ download_collinfo:
57101
curl -O https://index.commoncrawl.org/collinfo.json
58102

59103
CC-MAIN-2024-22.warc.paths.gz:
60-
@echo "downloading the list from S3 requires S3 auth (even though it is free)"
104+
@echo "!! this step requires authentication via S3 credentials (even though it is free)"
61105
@echo "note that this file should already be in the repo"
62106
aws s3 ls s3://commoncrawl/cc-index/table/cc-main/warc/crawl=CC-MAIN-2024-22/subset=warc/ | awk '{print $$4}' | gzip -9 > CC-MAIN-2024-22.warc.paths.gz
63107

README.md

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,39 @@ python ./warcio-iterator.py whirlwind.warc.wat.gz
174174

175175
The output has three sections, one each for the WARC, WET, and WAT. For each one, it prints the record types we saw before, plus the `WARC-Target-URI` for those record types that have it.
176176

177+
### Iterating over remote files
178+
179+
warcio also supports working on remote files, so let us try the same command on the remote version of the same WARC file we just iterated locally. We will reach this remote file from the Github repository for this tutorial:
180+
181+
`make iterate-remote`
182+
183+
<details>
184+
<summary>Click to view code</summary>
185+
186+
```
187+
python ./warcio-iterator.py https://raw.githubusercontent.com/commoncrawl/whirlwind-python/refs/heads/main/whirlwind.warc.gz
188+
```
189+
</details>
190+
191+
The output should be identical to what you saw from the local file:
192+
193+
<details>
194+
<summary>Click to view output</summary>
195+
196+
```
197+
WARC-Type: warcinfo
198+
WARC-Type: request
199+
WARC-Target-URI https://an.wikipedia.org/wiki/Escopete
200+
WARC-Type: response
201+
WARC-Target-URI https://an.wikipedia.org/wiki/Escopete
202+
WARC-Type: metadata
203+
WARC-Target-URI https://an.wikipedia.org/wiki/Escopete
204+
```
205+
</details>
206+
207+
We got the same output, but this time by streaming the file over HTTPS instead of reading from local disk. Later in the tour, we will use this capability to index and extract records from remote WARC files hosted on AWS S3 buckets.
208+
209+
177210
## Task 3: Index the WARC, WET, and WAT
178211

179212
The example WARC files we've been using are tiny and easy to work with. The real WARC files are around a gigabyte in size and contain about 30,000 webpages each. What's more, we have around 24 million of these files! To read all of them, we could iterate, but what if we wanted random access so we could read just one particular record? We do that with an index.
@@ -194,7 +227,7 @@ The CDX index files are sorted plain-text files, with each line containing infor
194227

195228
We can create our own CDXJ index from the local WARCs by running:
196229

197-
```make cdxj```
230+
`make cdxj`
198231

199232
This uses the [cdxj-indexer](https://github.com/webrecorder/cdxj-indexer) library to generate CDXJ index files for our WARC files by running the code below:
200233

@@ -216,7 +249,43 @@ For each of these records, there's one text line in the index - yes, it's a flat
216249

217250
What is the purpose of this funky format? It's done this way because these flat files (300 gigabytes total per crawl) can be sorted on the primary key using any out-of-core sort utility e.g. the standard Linux `sort`, or one of the Hadoop-based out-of-core sort functions.
218251

219-
The JSON blob has enough information to cleanly isolate the raw data of a single record: it defines which WARC file the record is in, and the byte offset and length of the record within this file. We'll use that in the next section.
252+
The JSON blob has enough information to cleanly isolate the raw data of a single record: it defines which WARC file the record is in, and the byte offset and length of the record within this file. We'll use that in Task 4, when accessing the contents of the WARC at this offset. But first, let's observe that we could do all of this processing over a remote file location, as before.
253+
254+
255+
### Indexing remote WARC files
256+
257+
Through warcio's remote file handling capabilities, `cdxj-indexer` too can work on remote files, and this is true not just Common Crawl's, but any WARC files accessible over HTTPS or S3. As an example, let us check two WARC files from the End-of-Term Web Archive, which preserves U.S. government websites around presidential transitions. We will check one WARC file crawled by the Internet Archive (in the IA-000 segment), and another one repackaged from Common Crawl data (in the CC-000 segment). Let's index a few records from each.
258+
259+
Run:
260+
`make cdxj-remote-https`
261+
262+
<details>
263+
<summary>Click to view code</summary>
264+
265+
```
266+
cdxj-indexer https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/IA-000/warc/EOT24PRE-20240926172119-crawl804_EOT24PRE-20240926172119-00000.warc.gz 2>/dev/null | head -n 10 | tee eot-ia.cdxj
267+
cdxj-indexer https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/CC-000/warc/EOT-2024-REPACKAGE-CC-MAIN-2024-42-GOV-000000-001.warc.gz 2>/dev/null | head -n 10 | tee eot-cc.cdxj
268+
```
269+
</details>
270+
271+
The first command fetches and indexes these two WARCs over HTTPS. Since they are both around 1GB each, we display and save only the first 10 records.
272+
273+
If you have AWS credentials configured, you can also access the same files over S3, which is faster when running on AWS. Even though you will need AWS credentials for authentication purposes, this process is still free of charge since these are public buckets.
274+
If you do not have AWS credentials, you can access the same information over HTTPS as described above.
275+
276+
Run:
277+
278+
`make cdxj-remote-s3`
279+
280+
<details>
281+
<summary>Click to view code</summary>
282+
283+
```
284+
cdxj-indexer s3://eotarchive/crawl-data/EOT-2024/segments/IA-000/warc/EOT24PRE-20240926172119-crawl804_EOT24PRE-20240926172119-00000.warc.gz 2>/dev/null | head -n 10 | tee eot-ia.cdxj
285+
cdxj-indexer s3://eotarchive/crawl-data/EOT-2024/segments/CC-000/warc/EOT-2024-REPACKAGE-CC-MAIN-2024-42-GOV-000000-001.warc.gz 2>/dev/null | head -n 10 | tee eot-cc.cdxj
286+
```
287+
</details>
288+
220289

221290
## Task 4: Use the CDXJ index to extract a subset of raw content from the local WARC, WET, and WAT
222291

@@ -233,7 +302,7 @@ and lengths for every record.
233302

234303
Let's extract some individual records from our warc.gz files. Run:
235304

236-
```make extract```
305+
`make extract`
237306

238307
to run a set of extractions from your local
239308
`whirlwind.*.gz` files with `warcio` using the code below:
@@ -256,6 +325,33 @@ ones as in the index. Look at the three output files: `extraction.html`, `extrac
256325

257326
Notice that we extracted HTML from the WARC, text from WET, and JSON from the WAT (as shown in the different file extensions). This is because the payload in each file type is formatted differently!
258327

328+
329+
### Extracting from remote WARC files
330+
331+
The same random access trick works on remote files. By indexing deeper into the EOT WARC files from Task 3 (try increasing the head count, or removing it entirely if you're patient), we can find offsets for specific records and extract them directly — without downloading the entire file.
332+
333+
Run:
334+
`make extract-remote-https`
335+
336+
<details>
337+
<summary>Click to view code</summary>
338+
339+
```
340+
warcio extract https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/IA-000/warc/EOT24PRE-20240926172119-crawl804_EOT24PRE-20240926172119-00000.warc.gz 50755
341+
warcio extract https://eotarchive.s3.amazonaws.com/crawl-data/EOT-2024/segments/CC-000/warc/EOT-2024-REPACKAGE-CC-MAIN-2024-42-GOV-000000-001.warc.gz 18595
342+
```
343+
</details>
344+
345+
The first command extracts the record for https://hpxml.nrel.gov/ (HPXML Toolbox, hosted by the National Renewable Energy Laboratory) from an Internet Archive crawl. The second extracts the record for https://before-you-ship.18f.gov/ (18F's pre-launch checklist for government services) from a Common Crawl repackage.
346+
347+
As with indexing, you can also use S3 paths if you have AWS credentials configured:
348+
349+
`make extract-remote-s3`
350+
351+
In both cases, warcio uses the byte offset to seek directly to the right position in the remote file and decompress just that one record. Later in this tutorial we will see the same mechanism being used by `cdx_toolkit` to fetch a specific capture, by looking up the offset in the CDX index, then making a byte-range request to retrieve just the record you want.
352+
353+
**Note:** If you look at the output of the second extraction (before-you-ship.18f.gov), you'll notice that despite having an HTTP 200 status in the index, the actual HTML content is just a redirect page pointing to handbook.tts.gsa.gov. This is a good reminder that real crawl data is messy — a 200 status in the index doesn't always mean you'll get a full page of content!
354+
259355
## Task 5: Wreck the WARC by compressing it wrong
260356

261357
As mentioned earlier, WARC/WET/WAT files look like they're normal gzipped files, but they're actually gzipped in a particular way that allows random access.
@@ -393,7 +489,7 @@ We check for capture results using the `cdxt` command `iter`, specifying the exa
393489
#### Retrieve the fetched content as WARC
394490

395491
Next, we use the `cdxt` command `warc` to retrieve the content and save it locally as a new WARC file, again specifying the exact URL, crawl identifier, and timestamp range. This creates the WARC file `TEST-000000.extracted.warc.gz` which contains a `warcinfo` record explaining what the WARC is, followed by the `response` record we requested.
396-
* If you dig into cdx_toolkit's code, you'll find that it is using the offset and length of the WARC record (as returned by the CDX index query) to make a HTTP byte range request to S3 that isolates and returns just the single record we want from the full file. It only downloads the response WARC record because our CDX index only has the response records indexed.
492+
* If you dig into `cdx_toolkit`'s code, you'll find that it is using the offset and length of the WARC record (as returned by the CDX index query) to make a HTTP byte range request to S3 that isolates and returns just the single record we want from the full file. It only downloads the response WARC record because our CDX index only has the response records indexed.
397493
* By default `cdxt` avoids overwriting existing files by automatically incrementing the counter in the filename. If you run this again without deleting `TEST-000000.extracted.warc.gz`, the data will be written again to a new file `TEST-000001.extracted.warc.gz`.
398494
* Limit, timestamp, and crawl index args, as well as URL wildcards, work as for `iter`.
399495

@@ -407,7 +503,7 @@ Now let's look at the columnar index, the other kind of index that Common Crawl
407503

408504
We could read the data directly from our index in our S3 bucket and analyse it in the cloud through AWS Athena. However, this is a managed service that costs money to use (though usually a small amount). [You can read about using it here.](https://commoncrawl.org/blog/index-to-warc-files-and-urls-in-columnar-format) This whirlwind tour will only use the free method of either fetching data from outside of AWS (which is kind of slow), or making a local copy of a single columnar index (300 gigabytes per monthly crawl), and then using that.
409505

410-
The columnar index is divided up into a separate index per crawl, which Athena or duckdb can stitch together. The cdx index is similarly divided up, but cdx_toolkit hides that detail from you.
506+
The columnar index is divided up into a separate index per crawl, which Athena or duckdb can stitch together. The cdx index is similarly divided up, but `cdx_toolkit` hides that detail from you.
411507

412508
For the purposes of this whirlwind tour, we don't want to configure all the crawl indices because it would be slow. So let's start by figuring out which crawl was ongoing on the date 20240518015810, and then we'll work with just that one crawl.
413509

@@ -598,8 +694,8 @@ All of these scripts run the same SQL query and should return the same record (w
598694

599695
1. Use the DuckDb techniques from [Task 8](#task-8-query-using-the-columnar-index--duckdb-from-outside-aws) and the [Index Server](https://index.commoncrawl.org) to find a new webpage in the archives.
600696
2. Note its url, warc, and timestamp.
601-
3. Now open up the Makefile from [Task 6](#task-6-use-cdx_toolkit-to-query-the-full-cdx-index-and-download-those-captures-from-aws-s3) and look at the actions from the cdx_toolkit section.
602-
4. Repeat the cdx_toolkit steps, but for the page and date range you found above.
697+
3. Now open up the Makefile from [Task 6](#task-6-use-cdx_toolkit-to-query-the-full-cdx-index-and-download-those-captures-from-aws-s3) and look at the actions from the `cdx_toolkit` section.
698+
4. Repeat the `cdx_toolkit` steps, but for the page and date range you found above.
603699

604700
## Congratulations!
605701

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
warcio
1+
warcio[s3]>=1.8.0
22
cdx_toolkit
33
duckdb
4+
fsspec
45
pyarrow
56
pandas
67
polars

warcio-iterator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import sys
44

5+
import fsspec
56
from warcio.archiveiterator import ArchiveIterator
67

78
for file in sys.argv[1:]:
8-
with open(file, 'rb') as stream:
9+
with fsspec.open(file, 'rb') as stream:
910
for record in ArchiveIterator(stream):
1011
print(' ', 'WARC-Type:', record.rec_type)
1112
if record.rec_type in {'request', 'response', 'conversion', 'metadata'}:

0 commit comments

Comments
 (0)