-
Notifications
You must be signed in to change notification settings - Fork 53
Optimize the OSS extraction process and implement Azure Blob extractor #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
1e480ba
2fe37b3
36d95ac
891979c
f669728
fb9c57e
6d0524f
1c393ef
7ec440f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package extractor | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/internetarchive/Zeno/internal/pkg/utils" | ||
| "github.com/internetarchive/Zeno/pkg/models" | ||
| ) | ||
|
|
||
| // All the supported object storage servers | ||
| var ObjectStorageServers = func() (s []string) { | ||
| s = append(s, s3CompatibleServers...) | ||
| s = append(s, azureServers...) | ||
| return s | ||
| }() | ||
|
|
||
| // IsObjectStorage checks if the response is from an object storage server | ||
| func IsObjectStorage(URL *models.URL) bool { | ||
| return utils.StringContainsSliceElements(URL.GetResponse().Header.Get("Server"), ObjectStorageServers) && | ||
| strings.Contains(URL.GetResponse().Header.Get("Content-Type"), "/xml") // tricky match both application/xml and text/xml | ||
|
yzqzss marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // ObjectStorage decides which helper to call based on the object storage server | ||
| func ObjectStorage(URL *models.URL) ([]*models.URL, error) { | ||
| defer URL.RewindBody() | ||
|
|
||
| server := URL.GetResponse().Header.Get("Server") | ||
| if utils.StringContainsSliceElements(server, s3CompatibleServers) { | ||
| return s3Compatible(URL) | ||
| } else if utils.StringContainsSliceElements(server, azureServers) { | ||
| return azure(URL) | ||
| } else { | ||
| return nil, fmt.Errorf("unknown object storage server: %s", server) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package extractor | ||
|
|
||
| import ( | ||
| "encoding/xml" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/internetarchive/Zeno/internal/pkg/log" | ||
| "github.com/internetarchive/Zeno/pkg/models" | ||
| ) | ||
|
|
||
| // Azure Blob Storage | ||
| var azureServers = []string{ | ||
| // Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 | ||
| "Windows-Azure-Blob", | ||
| // Blob Service Version 1.0 Microsoft-HTTPAPI/2.0 | ||
| "Blob Service Version", | ||
| // emulator, https://github.com/Azure/Azurite | ||
| "Azurite-Blob", | ||
| } | ||
|
Comment on lines
+12
to
+20
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tested Zeno with the azure extracor locally with Azurite and it works. But I haven't run it on a real Azure Blob yet. Also, I found that some Azure Blobs use the https://en.fofa.info/result?qbase64=aGVhZGVyPSJXaW5kb3dzLUF6dXJlLUJsb2Ii |
||
|
|
||
| // AzureBlobEnumerationResults represents the XML structure of an Azure Blob Storage listing | ||
| // <https://learn.microsoft.com/en-us/rest/api/storageservices/enumerating-blob-resources> | ||
| type AzureBlobEnumerationResults struct { | ||
| XMLName xml.Name `xml:"EnumerationResults"` | ||
| Prefix string `xml:"Prefix"` | ||
| Marker string `xml:"Marker"` | ||
| Blobs []AzureBlob `xml:"Blobs>Blob"` | ||
| NextMarker string `xml:"NextMarker"` | ||
| } | ||
|
|
||
| type AzureBlob struct { | ||
| Name string `xml:"Name"` // path/to/file.txt, no leading slash | ||
| LastModified string `xml:"Properties>Last-Modified"` | ||
| Size int64 `xml:"Properties>Content-Length"` | ||
| } | ||
|
|
||
| var azureLogger = log.NewFieldedLogger(&log.Fields{ | ||
| "component": "postprocessor.extractor.object_storage_azure", | ||
| }) | ||
|
|
||
| func azure(URL *models.URL) ([]*models.URL, error) { | ||
| defer URL.RewindBody() | ||
|
|
||
| // Decode XML result | ||
| var result AzureBlobEnumerationResults | ||
| if err := xml.NewDecoder(URL.GetBody()).Decode(&result); err != nil { | ||
| return nil, fmt.Errorf("error decoding AzureBlobEnumerationResults XML: %w", err) | ||
| } | ||
|
|
||
| reqURL := URL.GetRequest().URL | ||
|
|
||
| var outlinks []string | ||
|
|
||
| if result.NextMarker != "" { | ||
| nextURL := *reqURL | ||
| q := nextURL.Query() | ||
| q.Set("marker", result.NextMarker) | ||
| nextURL.RawQuery = q.Encode() | ||
| outlinks = append(outlinks, nextURL.String()) | ||
| } | ||
|
|
||
| // Build base url for files | ||
| // | ||
| // reqURL: "https://{endpoint_host}/{account}/{bucket}?..." | ||
| // -> | ||
| // baseURL: "https://{endpoint_host}/{account}/{bucket}/ | ||
| baseURL := *reqURL | ||
| baseURL.RawQuery = "" | ||
| baseURL.ForceQuery = false | ||
| if !strings.HasSuffix(baseURL.Path, "/") { | ||
| baseURL.Path = baseURL.Path + "/" | ||
| } | ||
|
|
||
| for _, blob := range result.Blobs { | ||
| fileURL := baseURL | ||
| if strings.HasPrefix(blob.Name, "/") { | ||
| azureLogger.Warn("invalid blob name: it starts with a leading slash", "blob_name", blob.Name) | ||
| continue | ||
| } | ||
| fileURL.Path = baseURL.Path + blob.Name | ||
|
yzqzss marked this conversation as resolved.
Outdated
yzqzss marked this conversation as resolved.
Outdated
|
||
| outlinks = append(outlinks, fileURL.String()) | ||
|
yzqzss marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return toURLs(outlinks), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package extractor | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestAzure(t *testing.T) { | ||
| t.Run("Valid XML with single object and nextMarker", func(t *testing.T) { | ||
| xmlBody := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <EnumerationResults ContainerName="zeno"> | ||
| <Prefix /> | ||
| <Marker>dir/azure_files/test_10.txt</Marker> | ||
| <MaxResults>1</MaxResults> | ||
| <Blobs> | ||
| <Blob> | ||
| <Name>dir/azure_files/test_100.txt</Name> | ||
| <Properties> | ||
| <Creation-Time>Thu, 15 May 2025 08:02:20 GMT</Creation-Time> | ||
| <Last-Modified>Thu, 15 May 2025 08:02:20 GMT</Last-Modified> | ||
| <Etag>0x202A0424CF3AFA0</Etag> | ||
| <Content-Length>4</Content-Length> | ||
| <Content-Type>text/plain</Content-Type> | ||
| <Content-MD5>kZ0ReVbTE1xMaD/wITUvXA==</Content-MD5> | ||
| <BlobType>BlockBlob</BlobType> | ||
| <LeaseStatus>unlocked</LeaseStatus> | ||
| <LeaseState>available</LeaseState> | ||
| <ServerEncrypted>true</ServerEncrypted> | ||
| <AccessTier>Hot</AccessTier> | ||
| <AccessTierInferred>true</AccessTierInferred> | ||
| <AccessTierChangeTime>Thu, 15 May 2025 08:02:20 GMT</AccessTierChangeTime> | ||
| </Properties> | ||
| </Blob> | ||
| </Blobs> | ||
| <NextMarker>dir/azure_files/test_100.txt</NextMarker> | ||
| </EnumerationResults>` | ||
| URLObj := buildTestObjectStorageURLObj("http://example.com/devstoreaccount1/zeno?restype=container&comp=list&maxresults=1", xmlBody, http.Header{"Server": []string{"Windows-Azure-Blob/1.0"}}) | ||
| outlinks, err := azure(URLObj) | ||
| outlinks2, err2 := ObjectStorage(URLObj) // indirectly call, for coverage testing | ||
| if err != nil || err2 != nil { | ||
| t.Fatalf("azure() returned unexpected error: %v, err2: %v", err, err2) | ||
| } | ||
|
|
||
| if len(outlinks) != 2 || len(outlinks2) != 2 { | ||
| t.Fatalf("expected 2 outlinks, got %d and %d", len(outlinks), len(outlinks2)) | ||
| } | ||
|
|
||
| expectedOutlinks := []string{ | ||
| "http://example.com/devstoreaccount1/zeno?comp=list&marker=dir%2Fazure_files%2Ftest_100.txt&maxresults=1&restype=container", | ||
| "http://example.com/devstoreaccount1/zeno/dir/azure_files/test_100.txt", | ||
| } | ||
|
|
||
| for i, outlink := range outlinks { | ||
| outlink.Parse() | ||
| if outlink.String() != expectedOutlinks[i] { | ||
| t.Errorf("expected %s, got %s", expectedOutlinks[i], outlink.String()) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("invalid XML", func(t *testing.T) { | ||
| xmlBody := `<EnumerationResults><BadTag` | ||
| URLObj := buildTestObjectStorageURLObj("http://example.com/devstoreaccount1/zeno?restype=container&comp=list&maxresults=1", xmlBody, http.Header{"Server": []string{"Windows-Azure-Blob/1.0"}}) | ||
| _, err := azure(URLObj) | ||
| if err == nil { | ||
| t.Fatalf("expected error for invalid XML, got none") | ||
| } | ||
|
|
||
| if !strings.Contains(err.Error(), "error decoding AzureBlobEnumerationResults XML") { | ||
| t.Fatalf("expected error 'error decoding AzureBlobEnumerationResults XML', got %v", err) | ||
| } | ||
| }) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.