Skip to content

Commit a9b53d5

Browse files
committed
Adding artifacts upload through S3/Minio
1 parent 6bf2d8e commit a9b53d5

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ authors = ["@deyandyankov, @pebeto, and contributors"]
44
version = "0.7.0"
55

66
[deps]
7+
AWSS3 = "1c724243-ef5b-51ab-93f4-b0a88ac62a95"
78
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
89
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
10+
FileTypes = "b58e86d0-4a47-4fce-a54d-8006a143ed90"
911
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1012
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
13+
Minio = "4281f0d9-7ae0-406e-9172-b7277c1efa20"
1114
ShowCases = "605ecd9f-84a6-4c9e-81e2-4798472b76a3"
1215
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
1316
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1417

1518
[compat]
19+
AWSS3 = "0.11.4"
1620
Base64 = "1.0"
21+
FileTypes = "0.1.1"
1722
HTTP = "1.0"
1823
JSON = "0.21"
24+
Minio = "0.2.2"
1925
ShowCases = "0.1"
2026
URIs = "1.0"
2127
julia = "1.0"

src/MLFlowClient.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ and artifacts. If you are not familiar with `MLFlow` and its concepts, please re
1111
"""
1212
module MLFlowClient
1313

14+
using AWSS3
1415
using Dates
16+
using FileTypes
1517
using UUIDs
1618
using HTTP
1719
using Base64
1820
using URIs
1921
using JSON
2022
using ShowCases
23+
using Minio
2124

2225
include("types/mlflow.jl")
2326
export MLFlow
@@ -65,7 +68,7 @@ export getrun, createrun, deleterun, setruntag, updaterun, restorerun, searchrun
6568
deleteruntag
6669

6770
include("services/logger.jl")
68-
export logbatch, loginputs, logmetric, logmodel, logparam
71+
export logbatch, loginputs, logmetric, logmodel, logparam, logartifact
6972

7073
include("services/artifact.jl")
7174
export listartifacts

src/services/logger.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,48 @@ function logmodel(instance::MLFlow, run_id::String, model_json::String)::Bool
139139
end
140140
logmodel(instance::MLFlow, run::Run, model_json::String)::Bool =
141141
logmodel(instance, run.info.run_id, model_json)
142+
143+
"""
144+
logartifact(s3_cfg::MinioConfig, run::Run, s3_cfg::MinioConfig, path::String="", artifact_name::String="")
145+
146+
Log artifact for a run. Supports only S3 buckets.
147+
148+
# Arguments
149+
- `s3_cfg`: Minio configuration
150+
- `Run`: ['Run'](@ref) instance
151+
- `path`: Path to the artifact
152+
- `artifact_name`: Name of the artifact in the bucket
153+
154+
# Returns
155+
`true` if successful. Otherwise, raises exception.
156+
157+
"""
158+
function logartifact(s3_cfg::MinioConfig, run::Run, path::String="", artifact_name::String="")
159+
# Parse the URI
160+
u = URI(run.info.artifact_uri)
161+
u.scheme == "s3" || ArgumentError("The artifact URI for the run has to be a S3 bucket. Got: $(run.info.artifact_uri)")
162+
isfile(path) || ArgumentError("Can not read file $(path).")
163+
bucket_name = u.host
164+
artifacts_base_path = u.path
165+
166+
167+
# Determine MIME type to use
168+
kind = matcher(path)
169+
mime_type_str = if isnothing(kind)
170+
@warn "FileTypes.jl could not determing the specific MIME type for $(path). Defaulting to application/octet-stream"
171+
"application/octet-stream"
172+
else
173+
string(kind.mime)
174+
end
175+
176+
# Read the bytes of the file
177+
content = read(path)
178+
179+
# Create the artifact path on the bucket
180+
artifact_path = isempty(artifact_name) ? joinpath(artifacts_base_path, path) : joinpath(artifacts_base_path, artifact_name)
181+
182+
s3_put(s3_cfg, bucket_name, artifact_path, content, mime_type_str)
183+
return true
184+
end
185+
186+

src/services/run.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ An instance of type [`Run`](@ref).
2020
function createrun(instance::MLFlow, experiment_id::String;
2121
run_name::Union{String,Missing}=missing, start_time::Union{Int64,Missing}=missing,
2222
tags::MLFlowUpsertData{Tag}=Tag[])::Run
23+
# Time returns system time in seconds, convert to ms.
24+
start_time = ismissing(start_time) ? Int(floor(1e3 * time())) : start_time
25+
2326
result = mlfpost(instance, "runs/create"; experiment_id=experiment_id,
2427
run_name=run_name, start_time=start_time, tags=parse(Tag, tags))
2528
return result["run"] |> Run

src/types/run.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ struct Run
140140
outputs::RunOutputs
141141
end
142142
Run(data::Dict{String,Any}) = Run(RunInfo(data["info"]), RunData(data["data"]),
143-
RunInputs(data["inputs"]), RunOutputs(data["outputs"]))
143+
RunInputs(data["inputs"]), RunOutputs(get(data, "outputs", Dict{String, Any}())))
144144
Base.show(io::IO, t::Run) = show(io, ShowCase(t, new_lines=true))

0 commit comments

Comments
 (0)