-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Error when uploading a file to ActiveStorage from a request body #3163
Comments
What versions of Rails and the SDK did you upgrade from? The behavior of the log formatter for file summary has not changed for a long time (I think in ~10 years). It is interesting that the file does not exist - I'm guessing it could still be valid since the logging is done after the request is actually sent (possibly reading the entire file triggers its deletion?). I'm not sure on the details on the rails/active storage side. |
It was Ruby 3.3.5 and Rails 7.1.3.2, but I've done a bit more testing and narrowed it down to the On the previous version, def upload
filename = params.require!(:filename)
p request.body.class
p request.body.read
p request.body.size
p File.exist?(request.body.path)
blob = ActiveStorage::Blob.create_and_upload!(io: request.body, filename:)
end
# => File
# => <the file contents>
# => 123456
# => false I don't understand what's going on here, because I don't see why it should be possible to write contents to a file without it existing on disk. I've created an issue in the Puma repo to see if there's a solution there. |
Yeah, thats strange - can you link the Puma issue here as well? |
Here is the discussion - puma/puma#3597 The "missing" file is completely legitimate - basically on linux you can create a Tempfile, and immediately remove the pointer from the filesystem, but it won't delete the data until the handle is closed. That being the case, we can still determine the file size from that file instance, so we could update the following code in the param formatter: # gems/aws-sdk-core/lib/aws-sdk-core/log/param_formatter.rb
def summarize_value(value)
case value
when String then summarize_string(value)
when Hash then '{' + summarize_hash(value) + '}'
when Array then summarize_array(value)
when File then summarize_file(value)
when Pathname then summarize_filepath(value)
else value.inspect
end
end
def summarize_file(file)
"#<File:#{file.path} (#{file.size} bytes)>"
end
def summarize_filepath(path)
"#<File:#{path} (#{File.size(path)} bytes)>"
end If that seems reasonable I can raise a PR. |
Describe the bug
I am receiving an uploaded file as the request body in a Rails request, which I am then forwarding directly to ActiveStorage which uses S3 as it's backend.
Regression Issue
Expected Behavior
The file should be uploaded correctly to S3.
Current Behavior
An error occurs when the AWS SDK is trying to create a log of the upload. Without too much digging, it appears that it is trying to retrieve file information for the request body, which may not be an actual file on disk, rather a different sort of I/O object.
Stack trace below:
Reproduction Steps
My controller code is as follows:
Possible Solution
I have patched the SDK with the following code as a workaround:
That produces log entries as follows:
Additional Information/Context
This has begun happening after upgrading to the latest version of Ruby, Rails and the SDK, so while I've marked it as a regression it may have been caused by Ruby or Rails.
Gem name ('aws-sdk', 'aws-sdk-resources' or service gems like 'aws-sdk-s3') and its version
aws-sdk-core 3.214.1, aws-sdk-s3 1.177.0
Environment details (Version of Ruby, OS environment)
Ruby 3.4.1, Rails 8.0.1
The text was updated successfully, but these errors were encountered: