Skip to content

MAINT Support relative blob paths in AzureBlobStorageIO#1478

Merged
romanlutz merged 3 commits intomicrosoft:mainfrom
biefan:fix-azure-blob-storage-relative-paths
Apr 12, 2026
Merged

MAINT Support relative blob paths in AzureBlobStorageIO#1478
romanlutz merged 3 commits intomicrosoft:mainfrom
biefan:fix-azure-blob-storage-relative-paths

Conversation

@biefan
Copy link
Copy Markdown
Contributor

@biefan biefan commented Mar 16, 2026

Summary

This updates AzureBlobStorageIO so that relative blob paths are handled consistently across blob operations.

Problem

Several AzureBlobStorageIO methods document or imply support for either:

  • a full blob URL, or
  • a relative blob path inside the configured container

In particular, read_file() explicitly documents relative-path support. However, the implementation always routed the input through parse_blob_url(), which only accepts full URLs and raises ValueError for relative paths.

That means calls such as:

await storage.read_file("dir1/dir2/sample.png")

fail immediately with ValueError("Invalid blob URL") even though the method contract says the relative path should be used as-is.

The same issue also affects path_exists() and is_file(), and it unnecessarily constrains write_file() to URL-only inputs as well.

Fix

  • Added a _resolve_blob_name helper that:
    • extracts the blob name from a full blob URL (via parse_blob_url), or
    • passes relative blob paths through unchanged
    • uses try/except around parse_blob_url to avoid redundant urlparse calls
    • normalizes backslashes to forward slashes so that Path objects created on Windows produce valid blob names
  • Used that helper in read_file(), write_file(), path_exists(), and is_file()
  • Updated write_file() docstring to document that both full URLs and relative paths are accepted
  • Documented intentional container name discard behavior in _resolve_blob_name docstring

This keeps the existing full-URL behavior intact while making the relative-path behavior match the documented API.

Tests

Added regression coverage for relative-path handling in all four affected methods:

  • read_file()
  • write_file()
  • path_exists()
  • is_file()

Added edge case tests for _resolve_blob_name:

  • Full URL resolution
  • Relative path passthrough
  • Simple filename
  • Backslash normalization
  • Path object handling

Validation command:

pytest tests/unit/models/test_storage_io.py -q

Validation result:

22 passed in 0.26s

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates AzureBlobStorageIO so callers can pass either a full Azure Blob URL or a relative blob path (within the configured container) consistently across blob operations.

Changes:

  • Added a _resolve_blob_name() helper to accept either full blob URLs or relative blob paths.
  • Updated read_file(), write_file(), path_exists(), and is_file() to use the helper instead of requiring URL parsing.
  • Added regression tests for relative-path handling in read_file() and path_exists().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
pyrit/models/storage_io.py Adds blob-name resolution helper and applies it across Azure blob operations to support relative paths.
tests/unit/models/test_storage_io.py Adds unit tests validating relative-path behavior for read_file() and path_exists().

You can also share your feedback on Copilot code review. Take the survey.

romanlutz and others added 2 commits April 11, 2026 17:41
…ths, expand tests

- Replace double urlparse with try/except around parse_blob_url
- Normalize backslashes to forward slashes for Windows Path safety
- Update write_file() docstring to document relative path support
- Add comment explaining intentional container name discard
- Add missing tests for write_file and is_file with relative paths
- Add edge case tests for _resolve_blob_name (backslashes, Path objects, etc.)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@romanlutz romanlutz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a theoretical problem since we're not using relative blob paths but nevertheless not a bad idea.

@romanlutz romanlutz changed the title Support relative blob paths in AzureBlobStorageIO MAINT Support relative blob paths in AzureBlobStorageIO Apr 12, 2026
@romanlutz romanlutz merged commit fd21b06 into microsoft:main Apr 12, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants