Compare commits

6 Commits

Author SHA1 Message Date
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
+75 -23
View File
@@ -14,11 +14,11 @@ using AWS, AWSS3
# this file use local AWS config
""" Example
const storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e", # key ID (create at garage-ui.yiem.cc)
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", # key ID's secret key
"sommpanion-s3"
storage = GarageStorage(
"https://s3-api.yiem.cc",
"GKb080154a2e5b19100b1b2c6e", # key ID (create at garage-ui.yiem.cc)
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", # key ID's secret key
"sommpanion-s3"
)
# 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' \
http://192.168.88.106:3902/users-1002.json
# test with API
data = String(get_file(storage, "users-1005.json"))
# 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
# ===================================================================
struct GarageStorage
config::GarageConfig
bucket::String
end
""" Create a thread-safe Garage S3 storage client.
# Arguments
@@ -128,16 +126,65 @@ end
# Examples
```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")
```
"""
struct GarageStorage
config::GarageConfig
bucket::String
end
function GarageStorage(endpoint::String, key::String, secret::String, bucket::String; region::String="garage")
creds = SimpleCredentials(key, secret, "")
config = GarageConfig(endpoint, region, creds)
return GarageStorage(config, bucket)
end
""" Upload an object to the Garage S3 bucket (callable struct).
# 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> 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
"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}})::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
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.
# Arguments
@@ -149,8 +196,8 @@ end
The data to upload.
# Return
- `Nothing` if the key contains slashes (upload aborted with warning).
- Prints a success message if the upload completes.
- `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
@@ -162,16 +209,21 @@ julia> put_file(storage, "test.json", "{\"status\": \"active\"}")
Successfully uploaded: test.json
```
"""
function put_file(storage::GarageStorage, 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(storage.config, storage.bucket, key, data)
println("Successfully uploaded: ", key)
function put_file(storage::GarageStorage, key::String, data::Union{String, Vector{UInt8}})::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
s3_put(storage.config, storage.bucket, key, data)
println("Successfully uploaded: ", key)
# file download URL
objURL = "$(storage.config.endpoint)/$(storage.bucket)/$key"
return objURL
end
""" Download an object from the Garage S3 bucket.