Skip to content

Commit

Permalink
Merge #260
Browse files Browse the repository at this point in the history
260: Robustly handle case variation in return value of s3_get_meta r=ericphanson a=haberdashPI

The case of keys in the return value `s3_get_meta` can vary. Sometime, in the same julia session, it will be "Content-Length" and other times it will be "content-length". 😡 I'm assuming this is an issue with S3 itself. 

To handle this situation I've written a small helper: `get_robust_case`. There are probably other places where it is useful, but I'm just fixing it for calls to `stat` here (where I often run into this problem).

closes #252
closes #246

Co-authored-by: David F Little <[email protected]>
Co-authored-by: David Little <[email protected]>
Co-authored-by: Eric Hanson <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2022
2 parents 067672b + 768bcbd commit 232b230
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AWSS3"
uuid = "1c724243-ef5b-51ab-93f4-b0a88ac62a95"
version = "0.9.6"
version = "0.9.7"

[deps]
AWS = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
Expand Down
9 changes: 8 additions & 1 deletion src/AWSS3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const SSDict = Dict{String,String}
const AbstractS3Version = Union{AbstractString,Nothing}
const AbstractS3PathConfig = Union{AbstractAWSConfig,Nothing}

# Utility function to workaround https://github.com/JuliaCloud/AWS.jl/issues/547
function get_robust_case(x, key)
lkey = lowercase(key)
haskey(x, lkey) && return x[lkey]
return x[key]
end

__init__() = FilePathsBase.register(S3Path)

# Declare new `parse` function to avoid type piracy
Expand Down Expand Up @@ -837,7 +844,7 @@ function s3_upload_part(
kwargs...,
)

return Dict(response.headers)["ETag"]
return get_robust_case(Dict(response.headers), "ETag")
end

function s3_complete_multipart_upload(
Expand Down
4 changes: 2 additions & 2 deletions src/s3path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ function Base.stat(fp::S3Path)

# Example: "Thu, 03 Jan 2019 21:09:17 GMT"
last_modified = DateTime(
resp["Last-Modified"][1:(end - 4)], dateformat"e, d u Y H:M:S"
get_robust_case(resp, "Last-Modified")[1:(end - 4)], dateformat"e, d u Y H:M:S"
)
s = parse(Int, resp["Content-Length"])
s = parse(Int, get_robust_case(resp, "Content-Length"))
blocks = ceil(Int, s / 4096)
end

Expand Down
8 changes: 8 additions & 0 deletions test/awss3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ function awss3_tests(config)
"ocaws.jl.test." * lowercase(Dates.format(now(Dates.UTC), df))
end

@testset "Robust key selection" begin
lower_dict = Dict("foo-bar" => 1)
upper_dict = Dict("Foo-Bar" => 1)
@test AWSS3.get_robust_case(lower_dict, "Foo-Bar") == 1
@test AWSS3.get_robust_case(upper_dict, "Foo-Bar") == 1
@test_throws KeyError("Foo-Bar") AWSS3.get_robust_case(Dict(), "Foo-Bar")
end

@testset "Create Bucket" begin
s3_create_bucket(config, bucket_name)
@test bucket_name in s3_list_buckets(config)
Expand Down

2 comments on commit 232b230

@ericphanson
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/64482

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.7 -m "<description of version>" 232b23058eb6f30949729d91d49132ca7bf9caba
git push origin v0.9.7

Please sign in to comment.