forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_push_pull.go
47 lines (40 loc) · 1.16 KB
/
simple_push_pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"context"
"fmt"
"io/ioutil"
"github.com/containerd/containerd/remotes/docker"
"github.com/shizhMSFT/oras/pkg/oras"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
ref := "localhost:5000/oras:test"
fileName := "hello.txt"
fileContent := []byte("Hello World!\n")
customMediaType := "my.custom.media.type"
ctx := context.Background()
resolver := docker.NewResolver(docker.ResolverOptions{})
// Push file(s) w custom mediatype to registry
pushContents := make(map[string]oras.Blob)
pushContents[fileName] = oras.Blob{
Content: fileContent,
MediaType: customMediaType,
}
fmt.Printf("Pushing %s to %s... ", fileName, ref)
err := oras.Push(ctx, resolver, ref, pushContents)
check(err)
fmt.Println("success!")
// Pull file(s) from registry and save to disk
fmt.Printf("Pulling from %s and saving to %s... ", ref, fileName)
allowedMediaTypes := []string{customMediaType}
pullContents, err := oras.Pull(ctx, resolver, ref, allowedMediaTypes...)
check(err)
err = ioutil.WriteFile(fileName, pullContents[fileName].Content, 0644)
check(err)
fmt.Println("success!")
fmt.Printf("Try running 'cat %s'\n", fileName)
}