slash works

This commit is contained in:
2026-08-29 09:24:30 +07:00
parent 2b2b7a378e
commit 5c1b70107a
2 changed files with 89 additions and 69 deletions
+8 -32
View File
@@ -126,17 +126,12 @@ end
# Arguments # Arguments
- `key::String` - `key::String`
The object key (name) in the bucket. Must not contain slashes (`/`). The object key (name) in the bucket.
- `data::Union{String, Vector{UInt8}}` - `data::Union{String, Vector{UInt8}}`
The data to upload. The data to upload.
# Return # Return
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs if the upload succeeds. - `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs.
- `nothing` if the key contains slashes (upload aborted with warning).
# Notes
- Slashes in keys are prohibited to prevent S3 listing issues. Use a flat naming
convention (e.g., `"users-1002.json"` instead of `"users/1002.json"`).
# Examples # Examples
```jldoctest ```jldoctest
@@ -154,14 +149,7 @@ struct put_file
storage::GarageStorage storage::GarageStorage
end end
function (pf::put_file)(key::String, data::Union{String, Vector{UInt8}} function (pf::put_file)(key::String, data::Union{String, Vector{UInt8}}
)::Union{NamedTuple{(:api, :web), Tuple{String, String}}, Nothing} )::NamedTuple{(:api, :web), Tuple{String, String}}
# Check if the key contains a slash (virtual folder character)
if occursin('/', key)
@warn "Upload aborted! Slashes ('/') are not allowed in keys ('$key') to prevent S3 listing issues. Use a flat naming convention instead (e.g., 'users-1002.json')."
return nothing
end
# Proceed if the key is flat
AWSS3.s3_put(pf.storage.config, pf.storage.bucket, key, data) AWSS3.s3_put(pf.storage.config, pf.storage.bucket, key, data)
println("Successfully uploaded: ", key) println("Successfully uploaded: ", key)
@@ -180,17 +168,12 @@ end
- `storage::GarageStorage` - `storage::GarageStorage`
The storage client instance. The storage client instance.
- `key::String` - `key::String`
The object key (name) in the bucket. Must not contain slashes (`/`). The object key (name) in the bucket.
- `data::Union{String, Vector{UInt8}}` - `data::Union{String, Vector{UInt8}}`
The data to upload. The data to upload.
# Return # Return
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs if the upload succeeds. - `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs.
- `nothing` if the key contains slashes (upload aborted with warning).
# Notes
- Slashes in keys are prohibited to prevent S3 listing issues. Use a flat naming
convention (e.g., `"users-1002.json"` instead of `"users/1002.json"`).
# Examples # Examples
```julia ```julia
@@ -203,14 +186,7 @@ julia> result.web
``` ```
""" """
function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}} function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}}
)::Union{NamedTuple{(:api, :web), Tuple{String, String}}, Nothing} )::NamedTuple{(:api, :web), Tuple{String, String}}
# Check if the key contains a slash (virtual folder character)
if occursin('/', key)
@warn "Upload aborted! Slashes ('/') are not allowed in keys ('$key') to prevent S3 listing issues. Use a flat naming convention instead (e.g., 'users-1002.json')."
return nothing
end
# Proceed if the key is flat
AWSS3.s3_put(storage.config, storage.bucket, key, data) AWSS3.s3_put(storage.config, storage.bucket, key, data)
println("Successfully uploaded: ", key) println("Successfully uploaded: ", key)
@@ -267,8 +243,8 @@ julia> keys = list_files(storage)
``` ```
""" """
function list_files(storage::GarageStorage) function list_files(storage::GarageStorage)
# Approach 2: s3_list_objects returns a Vector of Dicts with object details # Use delimiter="" to get all individual keys (no grouping by "directory")
objects = AWSS3.s3_list_objects(storage.config, storage.bucket) objects = AWSS3.s3_list_objects(storage.config, storage.bucket; delimiter="")
return [obj["Key"] for obj in objects] return [obj["Key"] for obj in objects]
end end
+81 -37
View File
@@ -118,14 +118,7 @@ using UUIDs: uuid4
@test storage isa GarageStorage @test storage isa GarageStorage
end end
# --- put_file callable struct (slash detection) --- # --- put_file callable struct ---
@testset "put_file callable struct returns nothing for keys with slashes" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
uploader = put_file(storage)
result = @test_logs (:warn, r"Upload aborted") uploader("a/b.json", "data")
@test result === nothing
end
@testset "put_file callable struct type is correct" begin @testset "put_file callable struct type is correct" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket") storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
@@ -139,32 +132,6 @@ using UUIDs: uuid4
@test uploader.storage === storage @test uploader.storage === storage
end end
# --- put_file function (slash detection) ---
@testset "put_file function returns nothing for keys with slashes" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
result = @test_logs (:warn, r"Upload aborted") put_file(storage, "a/b.json", "data")
@test result === nothing
end
@testset "put_file function returns nothing for keys with multiple slashes" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
result = @test_logs (:warn, r"Upload aborted") put_file(storage, "a/b/c/d.json", "data")
@test result === nothing
end
@testset "put_file function returns nothing for key with single slash in middle" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
result = @test_logs (:warn, r"Upload aborted") put_file(storage, "folder/file.json", "data")
@test result === nothing
end
@testset "put_file function returns nothing for key with trailing slash pattern" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
result = @test_logs (:warn, r"Upload aborted") put_file(storage, "a/b", "data")
@test result === nothing
end
# --- Integration tests with real Garage server --- # --- Integration tests with real Garage server ---
@testset "integration: full CRUD lifecycle" begin @testset "integration: full CRUD lifecycle" begin
@@ -219,15 +186,92 @@ using UUIDs: uuid4
@test deleted_data2 === nothing @test deleted_data2 === nothing
end end
@testset "integration: put_file rejects slashes even with real server" begin @testset "integration: nested key CRUD - single level" begin
storage = GarageStorage( storage = GarageStorage(
"https://s3-api.yiem.cc", "https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e", "GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", "a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket" "testbucket"
) )
result = @test_logs (:warn, r"Upload aborted") put_file(storage, "folder/test.json", "data")
@test result === nothing test_key = "single/slash/test-$(randstring(8)).json"
test_data = "{\"level\": 1, \"nested\": true}"
# Upload nested key
result = put_file(storage, test_key, test_data)
@test result isa NamedTuple
@test haskey(result, :api)
@test haskey(result, :web)
@test occursin("single/slash/", result.api)
@test occursin("single/slash/", result.web)
# Download and verify
downloaded = get_file(storage, test_key)
@test downloaded !== nothing
@test String(downloaded) == test_data
# Verify in listing
keys = list_files(storage)
@test test_key in keys
# Delete and verify gone
delete_file(storage, test_key)
@test get_file(storage, test_key) === nothing
end
@testset "integration: nested key CRUD - multi level" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "deep/nested/folder/subfolder/test-$(randstring(8)).json"
test_data = "{\"deeply\": \"nested\", \"path\": \"deep/nested/folder/subfolder/\"}"
# Upload deeply nested key
result = put_file(storage, test_key, test_data)
@test result isa NamedTuple
@test occursin("deep/nested/folder/subfolder/", result.api)
@test occursin("s3-api", result.api)
@test occursin("s3-web", result.web)
# Download and verify
downloaded = get_file(storage, test_key)
@test downloaded !== nothing
@test String(downloaded) == test_data
# Verify in listing
keys = list_files(storage)
@test test_key in keys
# Delete and verify gone
delete_file(storage, test_key)
@test get_file(storage, test_key) === nothing
end
@testset "integration: nested key with callable struct" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "callable/nested/test-$(randstring(8)).json"
test_data = "{\"callable\": true, \"nested\": true}"
uploader = put_file(storage)
result = uploader(test_key, test_data)
@test result isa NamedTuple
@test occursin("callable/nested/", result.api)
downloaded = get_file(storage, test_key)
@test String(downloaded) == test_data
delete_file(storage, test_key)
@test get_file(storage, test_key) === nothing
end end
@testset "integration: get_file returns nothing for missing key" begin @testset "integration: get_file returns nothing for missing key" begin