Skip to content

Commit 73c2a68

Browse files
samtkaplandeckerla
andauthored
add Base.cp methods (#31)
* add Base.cp methods Co-authored-by: Luke Decker <deckerla@gmail.com>
1 parent 1b9f9b8 commit 73c2a68

File tree

4 files changed

+183
-3
lines changed

4 files changed

+183
-3
lines changed

docs/src/example.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ serialize(container, "myblob.bin", (a=rand(10), b=rand(10)))
3535
# read and deserialze data from the container
3636
x = deserialize(container, "myblob.bin")
3737

38+
# copy a blob to a local file
39+
cp(container, "myblob.bin", "mylocalfile.bin")
40+
41+
# copy a local file to a blob
42+
cp("mylocalfile.bin", container, "myblob.bin")
43+
44+
# copy from a blob to another blob
45+
cp(container, "myblob.bin", container, "mycopyblob.bin")
46+
3847
# remove the container, and its contents
3948
rm(container)
4049
```
4150

4251
In addition, we can represent blob's, providing an API that is similar to handling POSIX files.
4352

4453
```julia
45-
# create a handle to a blob in a container
46-
io = open(AzContainer("foo"; storageaccount="mystorageaccount"), "myblob.bin")
47-
io = joinpath(AzContainer("foo"; storageaccount="mystorageaccount"), "myblob.bin") # this is equivalent to the previous line.
54+
# create a handle, io, to a blob, "myblob.bin", in a container, "foo", in storage account "mystorageaccount"
55+
io = open(AzContainer("foo"; storageaccount="mystorageaccount", session), "myblob.bin")
56+
io = joinpath(AzContainer("foo"; storageaccount="mystorageaccount", session), "myblob.bin") # this is equivalent to the previous line.
4857

4958
# write to the blob
5059
write(io, rand(10))
@@ -60,6 +69,18 @@ isfile(io)
6069
serialize(io, (a=rand(10),b=rand(10)))
6170
x = deserialize(io)
6271

72+
write(io, rand(10))
73+
74+
# copy a blob, io, to a local file, mylocalfile.bin
75+
cp(io, "mylocalfile.bin")
76+
77+
# copy a local file, mylocalfile.bin, to a blob, io
78+
cp("mylocalfile.bin", io)
79+
80+
# copy from a blob to another blob
81+
io2 = open(AzContainer("foo"; storageaccount="mystorageaccount", session), "mycopyblob.bin")
82+
cp(io, io2)
83+
6384
# remove the blob
6485
rm(io)
6586
```

docs/src/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ rm(::AzContainer)
1818

1919
## Blob methods
2020
```@docs
21+
cp(::AbstractString, ::AzContainer, ::AbstractString)
22+
cp(::AbstractString, ::AzStorage.AzObject)
2123
deserialize
2224
filesize
2325
isfile

src/AzStorage.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,72 @@ read and deserialize a blob `object::AzObject`. See `deserialize(container, "bl
502502
"""
503503
Serialization.deserialize(o::AzObject) = deserialize(o.container, o.name)
504504

505+
"""
506+
cp(from..., to...)
507+
508+
copy a blob to a local file, a local file to a blob, or a blob to a blob.
509+
510+
# Examples
511+
512+
## local file to blob
513+
```
514+
cp("localfile.txt", AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob.txt")
515+
```
516+
517+
## blob to local file
518+
```
519+
cp(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob.txt", "localfile.txt")
520+
```
521+
522+
## blob to blob
523+
```
524+
cp(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob_in.txt", AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob_out.txt")
525+
```
526+
"""
527+
function Base.cp(in::AbstractString, outc::AzContainer, outb::AbstractString)
528+
bytes = read!(in, Vector{UInt8}(undef,filesize(in)))
529+
write(outc, outb, bytes)
530+
end
531+
532+
function Base.cp(inc::AzContainer, inb::AbstractString, out::AbstractString)
533+
bytes = read!(inc, inb, Vector{UInt8}(undef, filesize(inc, inb)))
534+
write(out, bytes)
535+
end
536+
537+
function Base.cp(inc::AzContainer, inb::AbstractString, outc::AzContainer, outb::AbstractString)
538+
bytes = read!(inc, inb, Vector{UInt8}(undef, filesize(inc, inb)))
539+
write(outc, outb, bytes)
540+
end
541+
542+
"""
543+
cp(from, to)
544+
545+
copy a blob to a local file, a local file to a blob, or a blob to a blob.
546+
547+
# Examples
548+
549+
## local file to blob
550+
```
551+
cp("localfile.txt", open(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob.txt"))
552+
```
553+
554+
## blob to local file
555+
```
556+
cp(open(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob.txt"), "localfile.txt")
557+
```
558+
559+
## blob to blob
560+
```
561+
cp(open(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob_in.txt"), open(AzContainer("mycontainer";storageaccount="mystorageaccount"), "remoteblob_out.txt"))
562+
```
563+
"""
564+
Base.cp(in::AbstractString, out::AzObject) = cp(in, out.container, out.name)
565+
Base.cp(in::AzObject, out::AbstractString) = cp(in.container, in.name, out)
566+
Base.cp(in::AzObject, out::AzObject) = cp(in.container, in.name, out.container, out.name)
567+
568+
Base.cp(inc::AzContainer, inb::AbstractString, out::AzObject) = cp(inc, inb, out.container, out.name)
569+
Base.cp(in::AzObject, outc::AzContainer, outb::AbstractString) = cp(in.container, in.name, outc, outb)
570+
505571
"""
506572
readdir(container)
507573

test/runtests.jl

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,94 @@ end
396396
@test _c["prefix"] == ""
397397
@test length(_c) == 3
398398
end
399+
400+
@testset "Container, copy blob to local file" begin
401+
sleep(1)
402+
r = lowercase(randstring(MersenneTwister(millisecond(now())+26)))
403+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
404+
mkpath(c)
405+
write(c, "foo.txt", "Hello world")
406+
cp(c, "foo.txt", "foolocal.txt")
407+
@test read("foolocal.txt", String) == "Hello world"
408+
rm(c)
409+
rm("foolocal.txt")
410+
end
411+
412+
@testset "Container, copy local file to blob" begin
413+
sleep(1)
414+
write("foolocal.txt", "Hello world")
415+
r = lowercase(randstring(MersenneTwister(millisecond(now())+27)))
416+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
417+
mkpath(c)
418+
cp("foolocal.txt", c, "foo.txt")
419+
@test read(c, "foo.txt", String) == "Hello world"
420+
rm(c)
421+
rm("foolocal.txt")
422+
end
423+
424+
@testset "Container, copy blob to blob" begin
425+
sleep(1)
426+
r = lowercase(randstring(MersenneTwister(millisecond(now())+28)))
427+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
428+
mkpath(c)
429+
write(c, "foo.txt", "Hello world")
430+
cp(c, "foo.txt", c, "bar.txt")
431+
@test read(c, "bar.txt", String) == "Hello world"
432+
rm(c)
433+
end
434+
435+
@testset "Object, copy blob to local file" begin
436+
sleep(1)
437+
r = lowercase(randstring(MersenneTwister(millisecond(now())+29)))
438+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
439+
mkpath(c)
440+
write(open(c, "foo.txt"), "Hello world")
441+
cp(open(c, "foo.txt"), "foolocal.txt")
442+
@test read("foolocal.txt", String) == "Hello world"
443+
rm(c)
444+
rm("foolocal.txt")
445+
end
446+
447+
@testset "Object, copy local file to blob" begin
448+
sleep(1)
449+
write("foolocal.txt", "Hello world")
450+
r = lowercase(randstring(MersenneTwister(millisecond(now())+30)))
451+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
452+
mkpath(c)
453+
cp("foolocal.txt", open(c, "foo.txt"))
454+
@test read(open(c, "foo.txt"), String) == "Hello world"
455+
rm(c)
456+
rm("foolocal.txt")
457+
end
458+
459+
@testset "Object, copy blob to blob" begin
460+
sleep(1)
461+
r = lowercase(randstring(MersenneTwister(millisecond(now())+31)))
462+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
463+
mkpath(c)
464+
write(open(c, "foo.txt"), "Hello world")
465+
cp(open(c, "foo.txt"), open(c, "bar.txt"))
466+
@test read(open(c, "bar.txt"), String) == "Hello world"
467+
rm(c)
468+
end
469+
470+
@testset "Object,Container, copy blob to blob" begin
471+
sleep(1)
472+
r = lowercase(randstring(MersenneTwister(millisecond(now())+32)))
473+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
474+
mkpath(c)
475+
write(open(c, "foo.txt"), "Hello world")
476+
cp(open(c, "foo.txt"), c, "bar.txt")
477+
@test read(open(c, "bar.txt"), String) == "Hello world"
478+
rm(c)
479+
end
480+
481+
@testset "Container,Object, copy blob to blob" begin sleep(1)
482+
r = lowercase(randstring(MersenneTwister(millisecond(now())+33)))
483+
c = AzContainer("foo-$r-o", storageaccount=storageaccount, session=session, nthreads=2, nretry=10)
484+
mkpath(c)
485+
write(open(c, "foo.txt"), "Hello world")
486+
cp(c, "foo.txt", open(c, "bar.txt"))
487+
@test read(open(c, "bar.txt"), String) == "Hello world"
488+
rm(c)
489+
end

0 commit comments

Comments
 (0)