Compare commits

7 Commits

Author SHA1 Message Date
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
+85 -42
View File
@@ -1,30 +1,28 @@
module garageS3 module garageS3
export export
GarageStorage, GarageStorage, put_file, get_file, list_files, delete_file,
put_file, set_lifecycle_expiration, get_lifecycle, delete_lifecycle
get_file,
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( storage = 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" "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)
put_file(storage, "users-1005.json", "{\"status\": \"active\"}") GeneralUtils.put_file(storage, "users-1005.json", "{\"status\": \"active\"}")
keys = list_files(storage) keys = GeneralUtils.list_files(storage)
data = String(get_file(storage, "users-1004.json"))
println("Read back: ", data) println("Read back: ", data)
println("Bucket keys: ", keys) println("Bucket keys: ", keys)
@@ -34,9 +32,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(GeneralUtils.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 +103,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 +124,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 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
AWSS3.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
@@ -149,8 +194,8 @@ end
The data to upload. The data to upload.
# Return # Return
- `Nothing` if the key contains slashes (upload aborted with warning). - `String`: The file download URL if the upload succeeds.
- Prints a success message if the upload completes. - `nothing` if the key contains slashes (upload aborted with warning).
# Notes # Notes
- Slashes in keys are prohibited to prevent S3 listing issues. Use a flat naming - Slashes in keys are prohibited to prevent S3 listing issues. Use a flat naming
@@ -162,16 +207,21 @@ julia> put_file(storage, "test.json", "{\"status\": \"active\"}")
Successfully uploaded: test.json Successfully uploaded: 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}})::String
# Check if the key contains a slash (virtual folder character) # Check if the key contains a slash (virtual folder character)
if occursin('/', key) 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')." @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 return nothing
end end
# Proceed if the key is flat # Proceed if the key is flat
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)
# file download URL
objURL = "$(storage.config.endpoint)/$(storage.bucket)/$key"
return objURL
end end
""" Download an object from the Garage S3 bucket. """ Download an object from the Garage S3 bucket.
@@ -192,7 +242,7 @@ julia> data = String(get_file(storage, "test.json"))
``` ```
""" """
function get_file(storage::GarageStorage, key::String)::Vector{UInt8} function get_file(storage::GarageStorage, key::String)::Vector{UInt8}
return s3_get(storage.config, storage.bucket, key) return AWSS3.s3_get(storage.config, storage.bucket, key)
end end
""" List all object keys in the Garage S3 bucket. """ List all object keys in the Garage S3 bucket.
@@ -215,7 +265,7 @@ 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 # Approach 2: s3_list_objects returns a Vector of Dicts with object details
objects = s3_list_objects(storage.config, storage.bucket) objects = AWSS3.s3_list_objects(storage.config, storage.bucket)
return [obj["Key"] for obj in objects] return [obj["Key"] for obj in objects]
end end
@@ -233,7 +283,7 @@ 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
@@ -244,11 +294,4 @@ end
end # module GarageS3 end # module GarageS3