Skip to content
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

Use list_objects_v2 to check isdir #265

Merged
merged 8 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 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.8"
version = "0.9.9"
Copy link
Member

Choose a reason for hiding this comment

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

Technically we're changing behaviour that folks could depend on?

Suggested change
version = "0.9.9"
version = "0.10.0"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm somewhat unsure if that's the case, what behaviour could someone depend on? Theoretically you already need list_objects permissions to do an isdir on a non root bucket. I guess this could potentially be an issue for someone that is only doing an isdir on a root bucket somehow? I'm fine to bump the minor if you still think that should be the case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bumped the version regardless


[deps]
AWS = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
Expand All @@ -11,6 +11,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
FilePathsBase = "48062228-2e41-5def-b9a4-89aafe57970f"
MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d"
Mocking = "78c3b35d-d492-501b-9361-3d52fe80e533"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Retry = "20febd7b-183b-5ae2-ac4a-720e7ce64774"
SymDict = "2da68c74-98d7-5633-99d6-8493888d7b1e"
Expand All @@ -27,6 +28,7 @@ FilePathsBase = "=0.9.11, =0.9.12, =0.9.13, =0.9.14, =0.9.15"
JSON3 = "1"
MbedTLS = "0.6, 0.7, 1"
Minio = "0.1"
Mocking = "0.7"
OrderedCollections = "1"
Retry = "0.3, 0.4"
SymDict = "0.3"
Expand Down
15 changes: 8 additions & 7 deletions src/AWSS3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ export S3Path,
using AWS
using AWS.AWSServices: s3
using ArrowTypes
using Base64
using Compat: @something
using Dates
using EzXML
using FilePathsBase
using FilePathsBase: /, join
using Mocking
using OrderedCollections: OrderedDict, LittleDict
using SymDict
using Retry
using XMLDict
using EzXML
using Dates
using Base64
using UUIDs
using SymDict
using URIs
using Compat: @something
using UUIDs
using XMLDict

@service S3 use_response_type = true

Expand Down
14 changes: 12 additions & 2 deletions src/s3path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,18 @@ Base.isfile(fp::S3Path) = !fp.isdirectory && exists(fp)
function Base.isdir(fp::S3Path)
fp.isdirectory || return false
if isempty(fp.segments) # special handling of buckets themselves
# may not be super efficient for those with billions of buckets, but really our best option
fp.bucket ∈ s3_list_buckets(get_config(fp))
try
@mock S3.list_objects_v2(
fp.bucket, Dict("max-keys" => "0"); aws_config=get_config(fp)
)
return true
catch e
if ecode(e) == "NoSuchBucket"
return false
else
rethrow()
end
end
else
exists(fp)
end
Expand Down
11 changes: 7 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ using AWS
using AWS.AWSExceptions: AWSException
using AWSS3
using Arrow
using Test
using Dates
using Retry
using FilePathsBase
using FilePathsBase: /, join
using FilePathsBase.TestPaths
using UUIDs: uuid4
using FilePathsBase: /, join
using JSON3
using Mocking
mattBrzezinski marked this conversation as resolved.
Show resolved Hide resolved
using Retry
using Test
using UUIDs: uuid4

Mocking.activate()

@service S3 use_response_type = true

Expand Down
36 changes: 36 additions & 0 deletions test/s3path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,42 @@ function s3path_tests(config)
end
end

@testset "isdir" begin
# Top level
path = S3Path("s3://$(bucket_name)"; config=config)
@test isdir(path) == true

# No Such Bucket
test_exception = AWSException(
"NoSuchBucket",
"",
nothing,
AWS.HTTP.Exceptions.StatusError(404, "", "", ""),
nothing,
)
patch = @patch AWSS3.S3.list_objects_v2(args...; kwargs...) = throw(test_exception)

path = S3Path("s3://some_non_existent_bucket_7051649213"; config=config)
Copy link
Member

Choose a reason for hiding this comment

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

We should make this an actual AWSException w/ the NoSuchBucket error so we're not dependent on someone not making this an actual bucket.

apply(patch) do
@test isdir(path) == false
end

# Other Error
test_exception = AWSException(
"TestException",
"",
nothing,
AWS.HTTP.Exceptions.StatusError(404, "", "", ""),
nothing,
)
patch = @patch AWSS3.S3.list_objects_v2(args...; kwargs...) = throw(test_exception)

path = S3Path("s3://$(bucket_name)"; config=config)
apply(patch) do
@test_throws AWSException isdir(path)
end
end

mattBrzezinski marked this conversation as resolved.
Show resolved Hide resolved
@testset "JSON roundtripping" begin
json_path = S3Path("s3://$(bucket_name)/test_json"; config=config)
my_dict = Dict("key" => "value", "key2" => 5.0)
Expand Down