V0.6.2 #14
+8
-32
@@ -126,17 +126,12 @@ end
|
||||
|
||||
# Arguments
|
||||
- `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}}`
|
||||
The data to upload.
|
||||
|
||||
# Return
|
||||
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs if the upload succeeds.
|
||||
- `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"`).
|
||||
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs.
|
||||
|
||||
# Examples
|
||||
```jldoctest
|
||||
@@ -154,14 +149,7 @@ struct put_file
|
||||
storage::GarageStorage
|
||||
end
|
||||
function (pf::put_file)(key::String, data::Union{String, Vector{UInt8}}
|
||||
)::Union{NamedTuple{(:api, :web), Tuple{String, String}}, Nothing}
|
||||
# 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
|
||||
)::NamedTuple{(:api, :web), Tuple{String, String}}
|
||||
AWSS3.s3_put(pf.storage.config, pf.storage.bucket, key, data)
|
||||
println("Successfully uploaded: ", key)
|
||||
|
||||
@@ -180,17 +168,12 @@ end
|
||||
- `storage::GarageStorage`
|
||||
The storage client instance.
|
||||
- `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}}`
|
||||
The data to upload.
|
||||
|
||||
# Return
|
||||
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs if the upload succeeds.
|
||||
- `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"`).
|
||||
- `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs.
|
||||
|
||||
# Examples
|
||||
```julia
|
||||
@@ -203,14 +186,7 @@ julia> result.web
|
||||
```
|
||||
"""
|
||||
function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}}
|
||||
)::Union{NamedTuple{(:api, :web), Tuple{String, String}}, Nothing}
|
||||
# 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
|
||||
)::NamedTuple{(:api, :web), Tuple{String, String}}
|
||||
AWSS3.s3_put(storage.config, storage.bucket, key, data)
|
||||
println("Successfully uploaded: ", key)
|
||||
|
||||
@@ -267,8 +243,8 @@ julia> keys = list_files(storage)
|
||||
```
|
||||
"""
|
||||
function list_files(storage::GarageStorage)
|
||||
# Approach 2: s3_list_objects returns a Vector of Dicts with object details
|
||||
objects = AWSS3.s3_list_objects(storage.config, storage.bucket)
|
||||
# Use delimiter="" to get all individual keys (no grouping by "directory")
|
||||
objects = AWSS3.s3_list_objects(storage.config, storage.bucket; delimiter="")
|
||||
return [obj["Key"] for obj in objects]
|
||||
end
|
||||
|
||||
|
||||
+81
-37
@@ -118,14 +118,7 @@ using UUIDs: uuid4
|
||||
@test storage isa GarageStorage
|
||||
end
|
||||
|
||||
# --- put_file callable struct (slash detection) ---
|
||||
|
||||
@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
|
||||
# --- put_file callable struct ---
|
||||
|
||||
@testset "put_file callable struct type is correct" begin
|
||||
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
|
||||
@@ -139,32 +132,6 @@ using UUIDs: uuid4
|
||||
@test uploader.storage === storage
|
||||
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 ---
|
||||
|
||||
@testset "integration: full CRUD lifecycle" begin
|
||||
@@ -219,15 +186,92 @@ using UUIDs: uuid4
|
||||
@test deleted_data2 === nothing
|
||||
end
|
||||
|
||||
@testset "integration: put_file rejects slashes even with real server" begin
|
||||
@testset "integration: nested key CRUD - single level" begin
|
||||
storage = GarageStorage(
|
||||
"https://s3-api.yiem.cc",
|
||||
"GKb080154a2e5b19100b1b2c6e",
|
||||
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
|
||||
"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
|
||||
|
||||
@testset "integration: get_file returns nothing for missing key" begin
|
||||
|
||||
Reference in New Issue
Block a user