<p>I want to use <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">https://github.com/kubernetes/client-go</a> to copy a file from my file system to a container and vice versa.</p>

<pre><code>kubectl cp &lt;file-spec-src&gt; &lt;file-spec-dest&gt; -c &lt;specific-container&gt;
</code></pre>

<p>Is there a function in the go client that wraps the calls? Or can I use something like the <a href="https://godoc.org/k8s.io/client-go/kubernetes/typed/core/v1#CoreV1Interface.RESTClient" rel="nofollow noreferrer">RESTClient</a>?</p>

Kyroy
<p>Since the answer to this is pretty old, here is how I did it :</p>
<pre><code>package main

import (
  &quot;bytes&quot;
  &quot;fmt&quot;
  &quot;io&quot;
  &quot;k8s.io/apimachinery/pkg/runtime/schema&quot;
  &quot;k8s.io/apimachinery/pkg/runtime/serializer&quot;
  &quot;k8s.io/cli-runtime/pkg/genericclioptions&quot;
  &quot;k8s.io/client-go/kubernetes&quot;
  &quot;k8s.io/client-go/kubernetes/scheme&quot;
  &quot;k8s.io/client-go/rest&quot;
  &quot;k8s.io/kubectl/pkg/cmd/cp&quot;
  &quot;k8s.io/kubectl/pkg/cmd/exec&quot;
  &quot;log&quot;
  &quot;os&quot;
)

type PodExec struct {
  RestConfig *rest.Config
  *kubernetes.Clientset
}
func NewPodExec(config rest.Config, clientset *kubernetes.Clientset) *PodExec {
  config.APIPath = &quot;/api&quot; // Make sure we target /api and not just /
  config.GroupVersion = &amp;schema.GroupVersion{Version: &quot;v1&quot;} // this targets the core api groups so the url path will be /api/v1
  config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
  return &amp;PodExec{
    RestConfig: &amp;config,
    Clientset:  clientset,
  }  
}

func (p *PodExec) PodCopyFile(src string, dst string, containername string) (*bytes.Buffer, *bytes.Buffer, *bytes.Buffer, error) {
  ioStreams, in, out, errOut := genericclioptions.NewTestIOStreams()
  copyOptions := cp.NewCopyOptions(ioStreams)
  copyOptions.Clientset = p.Clientset
  copyOptions.ClientConfig = p.RestConfig
  copyOptions.Container = containername
  err := copyOptions.Run([]string{src, dst})
  if err != nil {
    return nil, nil, nil, fmt.Errorf(&quot;Could not run copy operation: %v&quot;, err)
  }
  return in, out, errOut, nil
}
</code></pre>
<p>You can then use PodCopyFile just like kubectl cp</p>
<pre><code>podExec := podexec.NewPodExec(*restconfig, clientset) // Here, you need to get your restconfig and clientset from either ~/.kube/config or built-in pod config.
_, out, _, err := podExec.PodCopyFile(&quot;/srcfile&quot;, &quot;/dstfile&quot;, &quot;containername&quot;)
if err != nil {
    fmt.Printf(&quot;%v\n&quot;, err)
}
fmt.Println(&quot;out:&quot;)
fmt.Printf(&quot;%s&quot;, out.String())
</code></pre>
