Last active
April 15, 2024 01:44
-
-
Save peter-wangxu/05ee416e47b1d9c987ae1af0b2cfbd16 to your computer and use it in GitHub Desktop.
image uploading with go-containerregistry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"compress/gzip" | |
"context" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
namer "github.com/google/go-containerregistry/pkg/name" | |
"github.com/google/go-containerregistry/pkg/v1/remote" | |
"github.com/google/go-containerregistry/pkg/v1/tarball" | |
) | |
// uploadImage uploads a tarball at `imagePath` (which should be a .tar.gz file) to a registry with `imageName`. | |
func uploadImage(imagePath, imageName string, decompressed bool) error { | |
ctx := context.Background() | |
tag, err := namer.NewTag(imageName) | |
if err != nil { | |
return fmt.Errorf("invalid image name %s: %v", imageName, err) | |
} | |
// imageLayer, err := tarball.LayerFromReader(gzipReader) | |
// if err != nil { | |
// return fmt.Errorf("unable to get layer from tarball: %v", err) | |
// } | |
fmt.Printf("Loading image from tarball\n") | |
// image, err := tarball.Image(func() (io.ReadCloser, error) { | |
// return io.NopCloser(tar.NewReader(file)), nil | |
// }, nil) | |
image, err := tarball.Image(func() (io.ReadCloser, error) { | |
file, _ := os.Open(imagePath) | |
// file.Seek(0, 0) | |
gzReader, err1 := gzip.NewReader(file) | |
return gzReader, err1 | |
}, nil) | |
if err != nil { | |
return fmt.Errorf("unable to create image from layer: %v", err) | |
} | |
fmt.Printf("Pushing image to %s/%s:%s\n", tag.RegistryStr(), tag.RepositoryStr(),tag.TagStr()) | |
if err := remote.Write(tag, image, remote.WithContext(ctx)); err != nil { | |
return fmt.Errorf("unable to push image to remote: %v", err) | |
} | |
fmt.Println("Image pushed successfully") | |
return nil | |
} | |
// 先解压到本地在上传 | |
func uploadImage2(imagePath, imageName string, decompressed bool) error { | |
ctx := context.Background() | |
tag, err := namer.NewTag(imageName) | |
if err != nil { | |
return fmt.Errorf("invalid image name %s: %v", imageName, err) | |
} | |
fmt.Printf("Opening %s\n", imagePath) | |
file, err := os.Open(imagePath) | |
if err != nil { | |
return fmt.Errorf("unable to open image file: %v", err) | |
} | |
defer file.Close() | |
gzipReader, err := gzip.NewReader(file) | |
if err != nil { | |
return fmt.Errorf("unable to create gzip reader: %v", err) | |
} | |
defer gzipReader.Close() | |
var tmpfile *os.File | |
if decompressed { | |
tmpfile, err = ioutil.TempFile("", "") | |
if err != nil { | |
return err | |
} | |
defer tmpfile.Close() | |
// 将解压的内容写入目标文件 | |
if _, err := io.Copy(tmpfile, gzipReader); err != nil { | |
return err | |
} | |
defer func() { | |
err := os.Remove(tmpfile.Name()) | |
if err != nil { | |
fmt.Printf("failed to remove temp file %s\n", tmpfile.Name()) | |
return | |
} | |
fmt.Printf("removed temp file %s\n", tmpfile.Name()) | |
}() | |
} | |
fmt.Printf("Loading image from tar.gz ball\n") | |
// image, err := tarball.Image(func() (io.ReadCloser, error) { | |
// return io.NopCloser(tar.NewReader(file)), nil | |
// }, nil) | |
image, err := tarball.ImageFromPath(tmpfile.Name(), nil) | |
if err != nil { | |
return fmt.Errorf("unable to create image from layer: %v", err) | |
} | |
fmt.Printf("Pushing image to %s/%s:%s\n", tag.RegistryStr(), tag.RepositoryStr(),tag.TagStr()) | |
if err := remote.Write(tag, image, remote.WithContext(ctx)); err != nil { | |
return fmt.Errorf("unable to push image to remote: %v", err) | |
} | |
fmt.Println("Image pushed successfully") | |
return nil | |
} | |
func main() { | |
// imagePath := "/Users/peter/Downloads/sss.tar" // Update this path, a .tar file but in .tar format | |
imagePath := "/Users/peter/Downloads/bbb.tar.gz" // Update this path, a .tar.gz file but in .tar.gz format | |
imageName := "127.0.0.1:5000/myorg/myimage:mytag" // Update this with the desired image name | |
if err := uploadImage(imagePath, imageName, true); err != nil { | |
fmt.Fprintf(os.Stderr, "failed to upload image: %v\n", err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment