-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1e480ba
feat: implement Azure Blob extractor
yzqzss 2fe37b3
refactor: make S3 a child function of object_storage
yzqzss 36d95ac
warning invalid blob name
yzqzss 891979c
refactor: clean up redundant code in s3 test
yzqzss f669728
return "unknown object storage server" error
yzqzss fb9c57e
add tests
yzqzss 6d0524f
chore
yzqzss 1c393ef
Update internal/pkg/postprocessor/extractor/utils.go
yzqzss 7ec440f
use url.JoinPath to simplify
yzqzss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(strings.ToLower(URL.GetResponse().Header.Get("Content-Type")), "/xml") // tricky match both application/xml and text/xml | ||
| } | ||
|
|
||
| // 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) | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
internal/pkg/postprocessor/extractor/object_storage_azure.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| 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", | ||
| } | ||
|
|
||
| // 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 | ||
| baseURL := *reqURL | ||
| baseURL.RawQuery = "" | ||
| baseURL.ForceQuery = false | ||
|
|
||
| for _, blob := range result.Blobs { | ||
| if strings.HasPrefix(blob.Name, "/") { | ||
| azureLogger.Warn("invalid blob name: it starts with a leading slash", "blob_name", blob.Name) | ||
| continue | ||
| } | ||
| fileURL := baseURL.JoinPath(blob.Name) | ||
| outlinks = append(outlinks, fileURL.String()) | ||
| } | ||
|
|
||
| return toURLs(outlinks), nil | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
internal/pkg/postprocessor/extractor/object_storage_azure_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
Server: Blob Service Version XXX...header, and I don't know what the difference is between them andServer: Windows-Azure-Blob....https://en.fofa.info/result?qbase64=aGVhZGVyPSJXaW5kb3dzLUF6dXJlLUJsb2Ii
https://en.fofa.info/result?qbase64=aGVhZGVyPSJCbG9iIFNlcnZpY2UgVmVyc2lvbiI%3D