This commit is contained in:
2026-08-28 07:47:12 +07:00
parent 517a08365d
commit 32cd21e1ee
+58 -11
View File
@@ -14,11 +14,11 @@ using AWS, AWSS3
# this file use local AWS config # this file use local AWS config
""" Example """ Example
const storage = GarageStorage( storage = 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" "sommpanion-s3"
) )
# 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)
@@ -34,9 +34,11 @@ curl -v \
-H 'Host: sommpanion-s3.s3-web.yiem.cc' \ -H 'Host: sommpanion-s3.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(get_file(storage, "users-1005.json"))
# test with a browser # test with a browser
https://s3-web.mydomain.com/my_bucket_name/users-1002.json https://s3-web.mydomain.com/my_bucket_name/users-1005.json
""" """
@@ -103,10 +105,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,16 +126,65 @@ 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 wrapper).
# Arguments
- `key::String`
The object key (name) in the bucket. Must not contain slashes (`/`).
- `data::Union{String, Vector{UInt8}}`
The data to upload.
# Return
- `String`: The file download URL 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"`).
# Examples
```jldoctest
julia> storage = GarageStorage("https://s3-api.yiem.cc", "GKb08015", "55114ff94828febca1", "sommpanion-s3")
julia> url = put_file(storage)("test.json", "{\"status\": \"active\"}")
Successfully uploaded: test.json
"https://s3-api.yiem.cc/sommpanion-s3/test.json"
```
"""
struct put_file
storage::GarageStorage
end
function (pf::put_file)(key::String, data::Union{String, Vector{UInt8}})
# 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
s3_put(pf.storage.config, pf.storage.bucket, key, data)
println("Successfully uploaded: ", key)
# file download URL
objURL = "$(pf.storage.config.endpoint)/$(pf.storage.bucket)/$key"
return objURL
end
""" Upload an object to the Garage S3 bucket. """ Upload an object to the Garage S3 bucket.
# Arguments # Arguments