diff --git a/Project.toml b/Project.toml index 7da150e..4814e00 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "AzStorage" uuid = "c6697862-1611-5eae-9ef8-48803c85c8d6" -version = "2.9.3" +version = "2.10.0" [deps] AbstractStorage = "14dbef02-f468-5f15-853e-5ec8dee7b899" diff --git a/src/AzStorage.jl b/src/AzStorage.jl index 8687a39..2deb972 100644 --- a/src/AzStorage.jl +++ b/src/AzStorage.jl @@ -1335,6 +1335,49 @@ Returns the size of the blob corresponding to `object::AzObject` """ Base.filesize(o::AzObject) = filesize(o.container, o.name) +""" + metadata(container, "blobname") + +Returns a dictionary corresponding to the metadata of the blob "blobname" in `container::AzContainer`. The dictionary contains the following keys: + +* "size": The size of the blob in bytes. +* "modified": The last modified date of the blob. +* "created": The creation date of the blob. +* "accessed": The last access date of the blob. +""" +function metadata(c::AzContainer, o::AbstractString) + r = @retry c.nretry HTTP.request( + "HEAD", + "https://$(c.storageaccount).blob.core.windows.net/$(c.containername)/$(addprefix(c,o))", + [ + "Authorization" => "Bearer $(token(c.session))", + "x-ms-version" => API_VERSION + ], + retry = false, + verbose = c.verbose, + connect_timeout = c.connect_timeout, + readtimeout = c.read_timeout) + + Dict( + "size" => parse(Int, HTTP.header(r, "Content-Length", "0")), + "modified" => tryparse(DateTime, HTTP.header(r, "Last-Modified", "not a date"), dateformat"e, dd u yyyy HH:MM:SS \G\M\T"), + "created" => tryparse(DateTime, HTTP.header(r, "x-ms-creation-time", "not a date"), dateformat"e, dd u yyyy HH:MM:SS \G\M\T"), + "accessed" => tryparse(DateTime, HTTP.header(r, "x-ms-last-access-time", "not a date"), dateformat"e, dd u yyyy HH:MM:SS \G\M\T") + ) +end + +""" + metadata(object::AzObject) + +Returns a dictionary corresponding to the metadata of the blob corresponding to `object::AzObject`. The dictionary contains the following keys: + +* "size": The size of the blob in bytes. +* "modified": The last modified date of the blob. +* "created": The creation date of the blob. +* "accessed": The last access date of the blob. +""" +metadata(o::AzObject) = metadata(o.container, o.name) + """ rm(container, "blobname") @@ -1540,6 +1583,6 @@ Returns the access tier for a blob `o::AzObject`. """ tier(o::AzObject) = tier(o.container, o.name) -export AzContainer, containers, readdlm, status, tier, tier!, writedlm +export AzContainer, containers, metadata, readdlm, status, tier, tier!, writedlm end diff --git a/test/runtests.jl b/test/runtests.jl index 1c112b5..43714b2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -220,6 +220,21 @@ end rm(c) end +@testset "Containers, metadata, prefix=$prefix" for prefix in ("", "prefix") + suffix = prefix == "" ? "-foo" : "-bar" + r = uuid4() + c = AzContainer("foo-$r-f$suffix", prefix=prefix, storageaccount=storageaccount, session=session) + c = robust_mkpath(c) + + write(c, "bar", rand(UInt8,10)) + + m = metadata(c, "bar") + @test m["size"] == 10 + @test isa(m["created"], DateTime) + @test isa(m["modified"], DateTime) || isa(m["modified"], Nothing) + @test isa(m["accessed"], DateTime) || isa(m["accessed"], Nothing) +end + @testset "Containers, bytes, nthreads=$nthreads, prefix=$prefix" for nthreads in (1, 2), prefix in ("","prefix") suffix = prefix == "" ? "-foo" : "-bar" r = uuid4()