Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc62098071 | |||
| 99996c1b04 | |||
| 4466320927 | |||
| ff757c19dd | |||
| c4741140f5 | |||
| 27703c3324 | |||
| 86b8582584 | |||
| 32cd21e1ee |
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
julia_version = "1.12.7"
|
||||
manifest_format = "2.0"
|
||||
project_hash = "4450296e6bb4e5e245de62f43b73b263b215aa5d"
|
||||
project_hash = "92e6bf163bcece135b000f14dcd39dd92d77a044"
|
||||
|
||||
[[deps.AWS]]
|
||||
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"]
|
||||
path = "."
|
||||
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
||||
version = "0.6.0"
|
||||
version = "0.6.1"
|
||||
|
||||
[[deps.GitHub]]
|
||||
deps = ["Base64", "Dates", "HTTP", "JSON", "MbedTLS", "Sockets", "SodiumSeal", "URIs"]
|
||||
|
||||
@@ -16,11 +16,13 @@ HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
|
||||
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
|
||||
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
|
||||
NATS = "55e73f9c-eeeb-467f-b4cc-a633fde63d2a"
|
||||
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
|
||||
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
|
||||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
|
||||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
|
||||
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
|
||||
StringDistances = "88034a9c-02f8-509d-84a9-84ec65e18404"
|
||||
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
|
||||
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
||||
|
||||
[compat]
|
||||
@@ -31,5 +33,7 @@ HTTP = "2.5.0 - 2.9.9"
|
||||
JSON = "1.3.0 - 1.9.9"
|
||||
LibPQ = "1.18.0"
|
||||
NATS = "0.1.0"
|
||||
OrderedCollections = "2.0.1"
|
||||
Revise = "3.13.2"
|
||||
StringDistances = "1.0.0"
|
||||
URIs = "1.7.0"
|
||||
|
||||
+73
-30
@@ -1,20 +1,19 @@
|
||||
module garageS3
|
||||
|
||||
export
|
||||
GarageStorage,
|
||||
put_file,
|
||||
get_file,
|
||||
list_files,
|
||||
delete_file
|
||||
GarageStorage, put_file, get_file, list_files, delete_file,
|
||||
set_lifecycle_expiration, get_lifecycle, delete_lifecycle
|
||||
|
||||
using AWS, AWSS3
|
||||
using AWS
|
||||
@service S3
|
||||
using AWSS3
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
# Garage uses Path-Style routing (https://s3-api.my-domain.com/bucket/key).
|
||||
# this file use local AWS config
|
||||
|
||||
""" Example
|
||||
const storage = GarageStorage(
|
||||
storage = GeneralUtils.GarageStorage(
|
||||
"https://s3-api.yiem.cc",
|
||||
"GKb080154a2e5b19100b1b2c6e", # key ID (create at garage-ui.yiem.cc)
|
||||
"a2c6b1379c2f731d3e6e5a408dd4d6cffca7511717675f55114ff94828febca1", # key ID's secret key
|
||||
@@ -22,9 +21,8 @@ const storage = GarageStorage(
|
||||
)
|
||||
|
||||
# Safe to run inside concurrent HTTP handlers (e.g., Oxygen.jl, HTTP.jl)
|
||||
put_file(storage, "users-1005.json", "{\"status\": \"active\"}")
|
||||
keys = list_files(storage)
|
||||
data = String(get_file(storage, "users-1004.json"))
|
||||
GeneralUtils.put_file(storage, "users-1005.json", "{\"status\": \"active\"}")
|
||||
keys = GeneralUtils.list_files(storage)
|
||||
|
||||
println("Read back: ", data)
|
||||
println("Bucket keys: ", keys)
|
||||
@@ -34,9 +32,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(GeneralUtils.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 +103,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 +124,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
|
||||
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.
|
||||
|
||||
# Arguments
|
||||
@@ -149,8 +194,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,7 +207,7 @@ julia> put_file(storage, "test.json", "{\"status\": \"active\"}")
|
||||
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)
|
||||
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')."
|
||||
@@ -170,8 +215,13 @@ function put_file(storage::GarageStorage, key::String, data::Union{String, Vecto
|
||||
end
|
||||
|
||||
# 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)
|
||||
|
||||
# file download URL
|
||||
objURL = "$(storage.config.endpoint)/$(storage.bucket)/$key"
|
||||
|
||||
return objURL
|
||||
end
|
||||
|
||||
""" 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}
|
||||
return s3_get(storage.config, storage.bucket, key)
|
||||
return AWSS3.s3_get(storage.config, storage.bucket, key)
|
||||
end
|
||||
|
||||
""" List all object keys in the Garage S3 bucket.
|
||||
@@ -215,7 +265,7 @@ julia> keys = list_files(storage)
|
||||
"""
|
||||
function list_files(storage::GarageStorage)
|
||||
# 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]
|
||||
end
|
||||
|
||||
@@ -233,7 +283,7 @@ julia> delete_file(storage, "test.json")
|
||||
```
|
||||
"""
|
||||
function delete_file(storage::GarageStorage, key::String)
|
||||
s3_delete(storage.config, storage.bucket, key)
|
||||
AWSS3.s3_delete(storage.config, storage.bucket, key)
|
||||
end
|
||||
|
||||
|
||||
@@ -244,11 +294,4 @@ end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module GarageS3
|
||||
|
||||
Reference in New Issue
Block a user