Compare commits

...

18 Commits

Author SHA1 Message Date
ton f88656172c object lifecycle works 2026-08-29 09:52:33 +07:00
ton 5c1b70107a slash works 2026-08-29 09:24:30 +07:00
ton 2b2b7a378e works 2026-08-29 09:00:21 +07:00
ton 3fb7457f61 1st commit 2026-08-29 08:22:46 +07:00
ton 1fc0f790d9 update 2026-08-28 19:48:29 +07:00
ton a559824faa update 2026-08-28 18:40:33 +07:00
ton d74635c47a update 2026-08-28 18:33:21 +07:00
ton 9d6a943503 update 2026-08-28 14:34:55 +07:00
ton c1c83eba77 update 2026-08-28 14:31:26 +07:00
ton dee5649254 update 2026-08-28 14:23:18 +07:00
ton fc62098071 update 2026-08-28 12:49:38 +07:00
ton 99996c1b04 update 2026-08-28 11:08:34 +07:00
ton 4466320927 update 2026-08-28 08:30:46 +07:00
ton ff757c19dd update 2026-08-28 08:29:07 +07:00
ton c4741140f5 update 2026-08-28 08:28:35 +07:00
ton 27703c3324 update docs 2026-08-28 08:25:21 +07:00
ton 86b8582584 update 2026-08-28 07:52:07 +07:00
ton 32cd21e1ee update 2026-08-28 07:47:12 +07:00
6 changed files with 654 additions and 102 deletions
+2 -2
View File
@@ -2,7 +2,7 @@
julia_version = "1.12.7" julia_version = "1.12.7"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "4450296e6bb4e5e245de62f43b73b263b215aa5d" project_hash = "92e6bf163bcece135b000f14dcd39dd92d77a044"
[[deps.AWS]] [[deps.AWS]]
deps = ["Base64", "Compat", "Dates", "Downloads", "GitHub", "HTTP", "IniFile", "JSON", "MD5", "Mocking", "OrderedCollections", "Random", "SHA", "ScopedValues", "Sockets", "URIs", "UUIDs", "XMLDict"] deps = ["Base64", "Compat", "Dates", "Downloads", "GitHub", "HTTP", "IniFile", "JSON", "MD5", "Mocking", "OrderedCollections", "Random", "SHA", "ScopedValues", "Sockets", "URIs", "UUIDs", "XMLDict"]
@@ -306,7 +306,7 @@ version = "1.2.0"
deps = ["AWS", "AWSS3", "CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "Graphs", "HTTP", "JSON", "LibPQ", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "StringDistances", "UUIDs"] deps = ["AWS", "AWSS3", "CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "Graphs", "HTTP", "JSON", "LibPQ", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "StringDistances", "UUIDs"]
path = "." path = "."
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.6.0" version = "0.6.1"
[[deps.GitHub]] [[deps.GitHub]]
deps = ["Base64", "Dates", "HTTP", "JSON", "MbedTLS", "Sockets", "SodiumSeal", "URIs"] deps = ["Base64", "Dates", "HTTP", "JSON", "MbedTLS", "Sockets", "SodiumSeal", "URIs"]
+5 -1
View File
@@ -1,6 +1,6 @@
name = "GeneralUtils" name = "GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.6.1" version = "0.6.2"
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"] authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
[deps] [deps]
@@ -16,11 +16,13 @@ HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1" LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
NATS = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a" NATS = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337" PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
StringDistances = "88034a9c-02f8-509d-84a9-84ec65e18404" StringDistances = "88034a9c-02f8-509d-84a9-84ec65e18404"
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[compat] [compat]
@@ -31,5 +33,7 @@ HTTP = "2.5.0 - 2.9.9"
JSON = "1.3.0 - 1.9.9" JSON = "1.3.0 - 1.9.9"
LibPQ = "1.18.0" LibPQ = "1.18.0"
NATS = "0.1.0" NATS = "0.1.0"
OrderedCollections = "2.0.1"
Revise = "3.13.2" Revise = "3.13.2"
StringDistances = "1.0.0" StringDistances = "1.0.0"
URIs = "1.7.0"
View File
+2
View File
@@ -0,0 +1,2 @@
# dummy main.jl so i can do GeneralUtils precompile
using GeneralUtils
+185 -63
View File
@@ -1,42 +1,44 @@
module garageS3 module garageS3
export export
GarageStorage, LifecycleExpiration, build_lifecycle_xml,
put_file, GarageStorage, put_file, get_file, list_files, delete_file,
get_file, set_lifecycle_expiration, get_lifecycle, delete_lifecycle
list_files,
delete_file
using AWS, AWSS3 using AWS
@service S3
using AWSS3
# ---------------------------------------------- 100 --------------------------------------------- # # ---------------------------------------------- 100 --------------------------------------------- #
# Garage uses Path-Style routing (https://s3-api.my-domain.com/bucket/key). # Garage uses Path-Style routing (https://s3-api.my-domain.com/bucket/key).
# this file use local AWS config # this file use local AWS config
""" Example """ Example
const storage = GarageStorage( storage1 = GeneralUtils.GarageStorage(
"https://s3-api.yiem.cc", "https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e", # key ID (create at garage-ui.yiem.cc) "GKb080154a2e5b19100b1b2c6e", # key ID (create at garage-ui.yiem.cc)
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", # key ID's secret key "a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", # key ID's secret key
"sommpanion-s3" "testbucket"
) )
# Safe to run inside concurrent HTTP handlers (e.g., Oxygen.jl, HTTP.jl) # Safe to run inside concurrent HTTP handlers (e.g., Oxygen.jl, HTTP.jl)
put_file(storage, "users-1005.json", "{\"status\": \"active\"}") # use UUID as filename because GarageS3 requires unique filename for each uploaded object
keys = list_files(storage) urls = GeneralUtils.put_file(storage1, "transferjob-(GeneralUtils.uuid4snakecase())", "{\"status\": \"inactive\"}")
data = String(get_file(storage, "users-1004.json")) key = GeneralUtils.list_files(storage)
println("Read back: ", data) println("Read back: ", data)
println("Bucket keys: ", keys) println("Bucket keys: ", key)
# test with curl # test with curl
curl -v \ curl -v \
-H 'Host: sommpanion-s3.s3-web.yiem.cc' \ -H 'Host: testbucket.s3-web.yiem.cc' \
http://192.168.88.106:3902/users-1002.json http://192.168.88.106:3902/users-1002.json
# test with API
data = String(GeneralUtils.get_file(storage, key[1]))
# test with a browser # test with a browser (urls.web)
https://s3-web.mydomain.com/my_bucket_name/users-1002.json https://s3-web.mydomain.com/my_bucket_name/users-1005.json
""" """
@@ -63,23 +65,6 @@ AWS.check_credentials(c::SimpleCredentials) = c
AWS.region(aws::GarageConfig) = aws.region AWS.region(aws::GarageConfig) = aws.region
AWS.credentials(aws::GarageConfig) = aws.credentials AWS.credentials(aws::GarageConfig) = aws.credentials
""" Generate the base URL for a Garage S3 service request.
# Arguments
- `aws::GarageConfig`
The Garage configuration containing the endpoint.
- `service::String`
The AWS service name (e.g., `"s3"`).
- `region::String`
The AWS region string.
# Return
- `String`: The stripped endpoint URL (trailing slashes removed).
"""
function AWS.generate_service_url(aws::GarageConfig, service::String, region::String)
return strip(aws.endpoint, '/')
end
""" Generate the full service URL including the resource path for a Garage S3 request. """ Generate the full service URL including the resource path for a Garage S3 request.
# Arguments # Arguments
@@ -103,10 +88,6 @@ end
# 2. Thread-Safe Storage Client Wrapper # 2. Thread-Safe Storage Client Wrapper
# =================================================================== # ===================================================================
struct GarageStorage
config::GarageConfig
bucket::String
end
""" Create a thread-safe Garage S3 storage client. """ Create a thread-safe Garage S3 storage client.
# Arguments # Arguments
@@ -128,50 +109,95 @@ end
# Examples # Examples
```jldoctest ```jldoctest
julia> storage = GarageStorage("https://s3-api.yiem.cc", "GKb080154a2e5b19100b1b2c6e", "a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", "sommpanion-s3") julia> storage = GarageStorage("https://s3-api.yiem.cc", "GKb08015", "55114ff94828febca1", "sommpanion-s3")
GarageStorage(GarageConfig(...), "sommpanion-s3") GarageStorage(GarageConfig(...), "sommpanion-s3")
``` ```
""" """
struct GarageStorage
config::GarageConfig
bucket::String
end
function GarageStorage(endpoint::String, key::String, secret::String, bucket::String; region::String="garage") function GarageStorage(endpoint::String, key::String, secret::String, bucket::String; region::String="garage")
creds = SimpleCredentials(key, secret, "") creds = SimpleCredentials(key, secret, "")
config = GarageConfig(endpoint, region, creds) config = GarageConfig(endpoint, region, creds)
return GarageStorage(config, bucket) return GarageStorage(config, bucket)
end end
""" Upload an object to the Garage S3 bucket (callable struct).
# Arguments
- `key::String`
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.
# Examples
```jldoctest
julia> storage1 = GarageStorage("https://s3-api.yiem.cc", "GKb08015", "55114ff94828febca1", "sommpanion-s3")
julia> put_file1 = put_file(storage1)
julia> downloadUrl = put_file1("test.json", "{\"status\": \"active\"}")
Successfully uploaded: test.json
julia> downloadUrl.api
"https://s3-api.yiem.cc/sommpanion-s3/test.json"
julia> downloadUrl.web
"https://s3-web.yiem.cc/sommpanion-s3/test.json"
```
"""
struct put_file
storage::GarageStorage
end
function (pf::put_file)(key::String, data::Union{String, Vector{UInt8}}
)::NamedTuple{(:api, :web), Tuple{String, String}}
AWSS3.s3_put(pf.storage.config, pf.storage.bucket, key, data)
println("Successfully uploaded: ", key)
# object url
api = "$(pf.storage.config.endpoint)/$(pf.storage.bucket)/$key"
# file download URL using browser
web = replace(api, "s3-api" => "s3-web")
return (api=api, web=web)
end
""" Upload an object to the Garage S3 bucket. """ Upload an object to the Garage S3 bucket.
# Arguments # Arguments
- `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
- `Nothing` if the key contains slashes (upload aborted with warning). - `NamedTuple{(:api, :web)}`: A tuple with `api` and `web` URLs.
- Prints a success message if the upload completes.
# 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
julia> put_file(storage, "test.json", "{\"status\": \"active\"}") julia> result = put_file(storage, "test.json", "{\"status\": \"active\"}")
Successfully uploaded: test.json Successfully uploaded: test.json
julia> result.api
"https://s3-api.yiem.cc/sommpanion-s3/test.json"
julia> result.web
"https://s3-web.yiem.cc/sommpanion-s3/test.json"
``` ```
""" """
function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}}) function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}}
# Check if the key contains a slash (virtual folder character) )::NamedTuple{(:api, :web), Tuple{String, String}}
if occursin('/', key) AWSS3.s3_put(storage.config, storage.bucket, key, data)
@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')." println("Successfully uploaded: ", key)
return nothing
end # object url
api = "$(storage.config.endpoint)/$(storage.bucket)/$key"
# Proceed if the key is flat
s3_put(storage.config, storage.bucket, key, data) # file download URL using browser
println("Successfully uploaded: ", key) web = replace(api, "s3-api" => "s3-web")
return (api=api, web=web)
end end
""" Download an object from the Garage S3 bucket. """ Download an object from the Garage S3 bucket.
@@ -191,8 +217,12 @@ julia> data = String(get_file(storage, "test.json"))
"\"{\\\"status\\\": \\\"active\\\"}\"" "\"{\\\"status\\\": \\\"active\\\"}\""
``` ```
""" """
function get_file(storage::GarageStorage, key::String)::Vector{UInt8} function get_file(storage::GarageStorage, key::String)::Union{Vector{UInt8}, Nothing}
return s3_get(storage.config, storage.bucket, key) try
return AWSS3.s3_get(storage.config, storage.bucket, key)
catch
return nothing
end
end end
""" List all object keys in the Garage S3 bucket. """ List all object keys in the Garage S3 bucket.
@@ -214,8 +244,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 = 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
@@ -233,22 +263,114 @@ julia> delete_file(storage, "test.json")
``` ```
""" """
function delete_file(storage::GarageStorage, key::String) function delete_file(storage::GarageStorage, key::String)
s3_delete(storage.config, storage.bucket, key) AWSS3.s3_delete(storage.config, storage.bucket, key)
end end
# ===================================================================
# 3. Bucket Lifecycle Configuration
# ===================================================================
""" Lifecycle expiration configuration for a bucket.
# Fields
- `prefix::String`: Object key prefix this rule applies to.
- `days::Int`: Number of days after object creation to expire/delete.
- `enabled::Bool`: Whether the rule is active (default: `true`).
- `id::String`: Unique identifier for the rule (default: auto-generated).
"""
struct LifecycleExpiration
prefix::String
days::Int
enabled::Bool
id::String
end
function LifecycleExpiration(prefix::String, days::Int; enabled::Bool=true, id::String="")
rule_id = isempty(id) ? "expire-" * replace(prefix, "/" => "_") * "-" * string(days) * "d" : id
return LifecycleExpiration(prefix, days, enabled, rule_id)
end
""" Build the XML body for a PutBucketLifecycleConfiguration request.
Supports a single rule with expiration by prefix.
"""
function build_lifecycle_xml(rule::LifecycleExpiration)::String
status = rule.enabled ? "Enabled" : "Disabled"
return """<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration>
<Rule>
<ID>$(rule.id)</ID>
<Filter>
<Prefix>$(rule.prefix)</Prefix>
</Filter>
<Status>$status</Status>
<Expiration>
<Days>$(rule.days)</Days>
</Expiration>
</Rule>
</LifecycleConfiguration>"""
end
""" Set bucket lifecycle expiration rule.
# Arguments
- `storage::GarageStorage`: The storage client instance.
- `rule::LifecycleExpiration`: The lifecycle expiration rule.
# Returns
- `Dict{String, Any}`: Parsed response from the server.
# Example
```julia
rule = LifecycleExpiration("uploads/", 30)
set_lifecycle_expiration(storage, rule)
```
"""
function set_lifecycle_expiration(storage::GarageStorage, rule::LifecycleExpiration)
xml_body = build_lifecycle_xml(rule)
args = Dict{String,Any}("body" => xml_body)
return S3.put_bucket_lifecycle_configuration(storage.bucket, args; aws_config=storage.config)
end
""" Get the bucket lifecycle configuration.
# Arguments
- `storage::GarageStorage`: The storage client instance.
# Returns
- `Dict{String, Any}`: Parsed lifecycle configuration, or `nothing` if no lifecycle is configured.
# Example
```julia
config = get_lifecycle(storage)
```
"""
function get_lifecycle(storage::GarageStorage)::Union{Dict{String,Any},Nothing}
try
return S3.get_bucket_lifecycle_configuration(storage.bucket; aws_config=storage.config)
catch e
if isa(e, AWS.AWSException) && e.code == "NoSuchLifecycleConfiguration"
return nothing
end
rethrow(e)
end
end
""" Delete the bucket lifecycle configuration.
# Arguments
- `storage::GarageStorage`: The storage client instance.
# Returns
- `Dict{String, Any}`: Parsed response from the server.
# Example
```julia
delete_lifecycle(storage)
```
"""
function delete_lifecycle(storage::GarageStorage)
return S3.delete_bucket_lifecycle(storage.bucket; aws_config=storage.config)
end
end # module GarageS3 end # module GarageS3
+460 -36
View File
@@ -1,39 +1,463 @@
using Test using Test
using GeneralUtils: detect_keyword using GeneralUtils
using GeneralUtils.garageS3
using AWS
using Random: randstring
using UUIDs: uuid4
@testset "garageS3.jl" begin
# --- SimpleCredentials ---
@testset "SimpleCredentials construction" begin
creds = GeneralUtils.garageS3.SimpleCredentials("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "sessiontoken")
@test creds.access_key_id == "AKIAIOSFODNN7EXAMPLE"
@test creds.secret_key == "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
@test creds.token == "sessiontoken"
end
@testset "SimpleCredentials AWS extensions" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key1", "secret1", "token1")
@test AWS.credentials(creds) === creds
@test AWS.check_credentials(creds) === creds
@test AWS.refresh!(creds; force=false) === creds
@test AWS.refresh!(creds) === creds
end
# --- GarageConfig ---
@testset "GarageConfig construction" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com", "us-east-1", creds)
@test config.endpoint == "https://s3-api.example.com"
@test config.region == "us-east-1"
@test config.credentials === creds
@test config isa AWS.AbstractAWSConfig
end
@testset "GarageConfig AWS extensions" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com", "garage", creds)
@test AWS.region(config) == "garage"
@test AWS.credentials(config) === creds
end
# --- AWS.generate_service_url ---
@testset "generate_service_url with resource starting with /" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com", "garage", creds)
url = AWS.generate_service_url(config, "s3", "/mybucket/key")
@test url == "https://s3-api.example.com/mybucket/key"
end
@testset "generate_service_url with resource NOT starting with /" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com", "garage", creds)
url = AWS.generate_service_url(config, "s3", "mybucket/key")
@test url == "https://s3-api.example.com/mybucket/key"
end
@testset "generate_service_url strips trailing slashes from endpoint" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com/", "garage", creds)
url = AWS.generate_service_url(config, "s3", "/bucket/key")
@test url == "https://s3-api.example.com/bucket/key"
end
@testset "generate_service_url with multiple trailing slashes" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("https://s3-api.example.com///", "garage", creds)
url = AWS.generate_service_url(config, "s3", "/bucket/key")
@test url == "https://s3-api.example.com/bucket/key"
end
@testset "generate_service_url preserves http endpoint" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("http://localhost:3900", "garage", creds)
url = AWS.generate_service_url(config, "s3", "/bucket/key")
@test url == "http://localhost:3900/bucket/key"
end
@testset "generate_service_url with http endpoint and trailing slash" begin
creds = GeneralUtils.garageS3.SimpleCredentials("key", "secret", "")
config = GeneralUtils.garageS3.GarageConfig("http://localhost:3900/", "garage", creds)
url = AWS.generate_service_url(config, "s3", "/bucket/key")
@test url == "http://localhost:3900/bucket/key"
end
# --- GarageStorage ---
@testset "GarageStorage default region" begin
storage = GarageStorage("https://s3-api.example.com", "key123", "secret123", "mybucket")
@test storage.bucket == "mybucket"
@test storage.config.region == "garage"
@test storage.config.endpoint == "https://s3-api.example.com"
end
@testset "GarageStorage custom region" begin
storage = GarageStorage("https://s3-api.example.com", "key123", "secret123", "mybucket"; region="custom-region")
@test storage.config.region == "custom-region"
end
@testset "GarageStorage endpoint without https" begin
storage = GarageStorage("http://localhost:3900", "key", "secret", "mybucket")
@test storage.config.endpoint == "http://localhost:3900"
end
@testset "GarageStorage credentials are SimpleCredentials" begin
storage = GarageStorage("https://s3-api.example.com", "mykey", "mysecret", "mybucket")
@test storage.config.credentials isa GeneralUtils.garageS3.SimpleCredentials
@test storage.config.credentials.access_key_id == "mykey"
@test storage.config.credentials.secret_key == "mysecret"
@test storage.config.credentials.token == ""
end
@testset "GarageStorage returns correct struct type" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
@test storage isa GarageStorage
end
# --- put_file callable struct ---
@testset "put_file callable struct type is correct" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
uploader = put_file(storage)
@test uploader isa put_file
end
@testset "put_file callable struct construction stores storage" begin
storage = GarageStorage("https://s3-api.example.com", "key", "secret", "bucket")
uploader = put_file(storage)
@test uploader.storage === storage
end
# --- Integration tests with real Garage server ---
@testset "integration: full CRUD lifecycle" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "testrunner-$(randstring(8)).json"
test_data = "{\"status\": \"testing\", \"runner\": \"runtests\"}"
# Test put_file(storage, key, data) - successful upload
result = put_file(storage, test_key, test_data)
@test result !== nothing
@test haskey(result, :api)
@test haskey(result, :web)
@test occursin("testbucket", result.api)
@test occursin("s3-api", result.api)
@test occursin("s3-web", result.web)
# Test get_file - retrieve the uploaded data
downloaded = get_file(storage, test_key)
@test downloaded !== nothing
@test downloaded isa Vector{UInt8}
@test String(downloaded) == test_data
# Test list_files - verify key appears in listing
keys = list_files(storage)
@test test_key in keys
# Test put_file callable struct
test_key2 = "testrunner-callable-$(randstring(8)).json"
callable_result = put_file(storage)(test_key2, "callable-data")
@test callable_result !== nothing
@test callable_result.api != ""
@test callable_result.web != ""
# Verify callable upload
downloaded2 = get_file(storage, test_key2)
@test String(downloaded2) == "callable-data"
# Test delete_file
delete_file(storage, test_key)
deleted_data = get_file(storage, test_key)
@test deleted_data === nothing
# Cleanup callable test object
delete_file(storage, test_key2)
deleted_data2 = get_file(storage, test_key2)
@test deleted_data2 === nothing
end
@testset "integration: nested key CRUD - single level" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
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
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
result = get_file(storage, "nonexistent-key-$(randstring(8)).json")
@test result === nothing
end
@testset "integration: binary data upload and download" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "testrunner-binary-$(randstring(8)).bin"
binary_data = UInt8[0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x48, 0x65, 0x6c, 0x6c, 0x6f]
result = put_file(storage, test_key, binary_data)
@test result !== nothing
downloaded = get_file(storage, test_key)
@test downloaded == binary_data
delete_file(storage, test_key)
@test get_file(storage, test_key) === nothing
end
@testset "integration: list_files returns vector of strings" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "testrunner-list-$(randstring(8)).txt"
put_file(storage, test_key, "list-test-data")
keys = list_files(storage)
@test keys isa Vector{String}
@test test_key in keys
delete_file(storage, test_key)
end
@testset "integration: url format validation" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
test_key = "testrunner-url-$(randstring(8)).json"
result = put_file(storage, test_key, "url-test")
@test startswith(result.api, "https://s3-api.yiem.cc/testbucket/")
@test occursin("/$test_key", result.api)
@test startswith(result.web, "https://s3-web.yiem.cc/testbucket/")
@test occursin("/$test_key", result.web)
delete_file(storage, test_key)
end
# --- Lifecycle configuration tests ---
@testset "LifecycleExpiration construction with default ID" begin
rule = LifecycleExpiration("uploads/", 30)
@test rule.prefix == "uploads/"
@test rule.days == 30
@test rule.enabled == true
@test rule.id == "expire-uploads_-30d"
end
@testset "LifecycleExpiration construction with custom ID" begin
rule = LifecycleExpiration("data/", 7; id="my-rule")
@test rule.prefix == "data/"
@test rule.days == 7
@test rule.enabled == true
@test rule.id == "my-rule"
end
@testset "LifecycleExpiration with disabled flag" begin
rule = LifecycleExpiration("temp/", 1; enabled=false)
@test rule.enabled == false
end
@testset "LifecycleExpiration with nested prefix" begin
rule = LifecycleExpiration("deep/nested/", 90)
@test rule.prefix == "deep/nested/"
end
@testset "build_lifecycle_xml generates valid XML" begin
rule = LifecycleExpiration("test/", 14; id="test-rule")
xml = build_lifecycle_xml(rule)
@test occursin("<?xml version=\"1.0\"", xml)
@test occursin("<LifecycleConfiguration>", xml)
@test occursin("<ID>test-rule</ID>", xml)
@test occursin("<Prefix>test/</Prefix>", xml)
@test occursin("<Status>Enabled</Status>", xml)
@test occursin("<Days>14</Days>", xml)
@test occursin("</LifecycleConfiguration>", xml)
end
@testset "build_lifecycle_xml generates disabled rule XML" begin
rule = LifecycleExpiration("temp/", 1; enabled=false)
xml = build_lifecycle_xml(rule)
@test occursin("<Status>Disabled</Status>", xml)
end
@testset "integration: lifecycle CRUD - set and get" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
# Should return nothing when no lifecycle is set
lc = get_lifecycle(storage)
@test lc === nothing
# Set lifecycle
rule = LifecycleExpiration("lifecycle-test/", 45)
set_lifecycle_expiration(storage, rule)
# Should return configuration after setting
lc = get_lifecycle(storage)
@test lc !== nothing
@test haskey(lc, "Rule")
# Delete lifecycle
delete_lifecycle(storage)
# Should return nothing after deletion
lc = get_lifecycle(storage)
@test lc === nothing
end
@testset "integration: lifecycle with nested prefix" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
rule = LifecycleExpiration("a/b/c/", 20; id="nested-rule")
set_lifecycle_expiration(storage, rule)
lc = get_lifecycle(storage)
@test lc !== nothing
@test haskey(lc, "Rule")
delete_lifecycle(storage)
@test get_lifecycle(storage) === nothing
end
@testset "integration: lifecycle enabled/disabled toggle" begin
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e",
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1",
"testbucket"
)
# Set enabled rule
rule1 = LifecycleExpiration("toggle/", 10; enabled=true)
set_lifecycle_expiration(storage, rule1)
lc = get_lifecycle(storage)
@test lc !== nothing
# Override with disabled rule
rule2 = LifecycleExpiration("toggle/", 10; enabled=false)
set_lifecycle_expiration(storage, rule2)
lc = get_lifecycle(storage)
@test lc !== nothing
delete_lifecycle(storage)
end
@testset "detect_keyword tests" begin
@test detect_keyword(["test"], "this is a test") == Dict("test" => 1)
@test detect_keyword(["hello", "world"], "hello world hello") == Dict("hello" => 2, "world" => 1)
@test detect_keyword(["cat"], "category") == Dict("cat" => 1)
@test detect_keyword(["cat"], "category"; mode="individual") == Dict("cat" => 0)
@test detect_keyword(["dog"], "dogs and cats"; mode="individual", delimiter=[' ']) == Dict("dog" => 0)
@test detect_keyword(["test"], "test.case"; mode="individual", delimiter=['.']) == Dict("test" => 1)
@test detect_keyword(["word"], "") == Dict("word" => 0)
@test detect_keyword(String[], "some text") == Dict{String, Integer}()
@test detect_keyword(["a", "b"], "a.b\nc"; delimiter=['.', '\n']) == Dict("a" => 1, "b" => 1)
multiline_text = """
first line
second line
first word
"""
@test detect_keyword(["first"], multiline_text) == Dict("first" => 2)
@test detect_keyword(["word"], "word"; mode="individual") == Dict("word" => 1)
@test detect_keyword(["test"], "testing.test.tester"; mode="individual", delimiter=['.']) == Dict("test" => 1)
end end