Skip to content

Use bytesize to get content size #40

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 2 commits into from
May 31, 2025
Merged

Use bytesize to get content size #40

merged 2 commits into from
May 31, 2025

Conversation

tr4b4nt
Copy link

@tr4b4nt tr4b4nt commented May 30, 2025

First of all thanks for this gem! We were really happy to find replacement for the abandoned azure-storage-blob.

After upgrading to this gem we started seeing weird 403 errors from time to time. The problem turned out to be with multi-byte UTF-8 characters and Content-Length header. Azure returns rather cryptic error when Content-Length header does not match actual length of the content (the error is included at the end).

Here is simple code to reproduce the problem:

client = AzureBlob::Client.new(...)
client.create_append_blob "test"
client.append_blob_block "test", "Ň"

Redacted Azure error response:

<Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'xxx' is not th
e same as any computed signature. Server used following string to sign: 'xxx'.</AuthenticationErrorDetail></Error>

Copy link
Member

@JoeDupuis JoeDupuis left a comment

Choose a reason for hiding this comment

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

Nice catch! Thank you for opening the PR.
There is a small issue. But everything else looks good!

Comment on lines 410 to 412
def content_size(content)
content.bytesize
end
Copy link
Member

Choose a reason for hiding this comment

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

This will break for StringIO.

Here's what Microsoft did on the old gem:
https://github.com/Azure/azure-storage-ruby/blob/master/common/lib/azure/core/auth/shared_key.rb#L62-L66

Suggested change
def content_size(content)
content.bytesize
end
def content_size(content)
if content.respond_to?(:bytesize)
content.bytesize
else
content.size
end
end

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the pointer. Fixed

@JoeDupuis JoeDupuis merged commit b6fa926 into testdouble:main May 31, 2025
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.

2 participants