object lifecycle works
This commit is contained in:
+100
@@ -1,6 +1,7 @@
|
||||
module garageS3
|
||||
|
||||
export
|
||||
LifecycleExpiration, build_lifecycle_xml,
|
||||
GarageStorage, put_file, get_file, list_files, delete_file,
|
||||
set_lifecycle_expiration, get_lifecycle, delete_lifecycle
|
||||
|
||||
@@ -266,11 +267,110 @@ function delete_file(storage::GarageStorage, key::String)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user