<p>I am trying to copy files from a <strong>Kubernetes</strong> pod to a <strong>GCP Bucket</strong>.
I can get the path of my file, but I was wondering if I want to do this programmatically using python, how can I do this.</p>
<p>I get my buckets using <code>gcsfs</code>. How can I copy a file in my program without using <code>kubectl</code>?</p>
<p>Is there anyway to do this through python.</p>

<p>I need to agree with the comment made by @anemyte:</p>
<blockquote>
<p>There is <a href="https://stackoverflow.com/questions/59703610/copy-file-from-pod-to-host-by-using-kubernetes-python-client">this question</a> about how to copy a file from a pod. You can download it and then use your code to upload it to the bucket.</p>
</blockquote>
<hr />
<p>I see 2 possible solutions to this question:</p>
<ul>
<li>Use <code>GCS Fuse</code> and Python code to copy the file from your <code>Pod</code> to <code>GCS</code> bucket</li>
<li>Use the Python library to connect to the GCS bucket without <code>gcsfuse</code></li>
</ul>
<hr />
<h3>Use <code>GCS Fuse</code> and Python code to copy the file from your <code>Pod</code> to <code>GCS</code> bucket</h3>
<p>Assuming that you have a <code>Pod</code> that was configured with <code>GCS Fuse</code> and it's working correctly you can use a following code snippet to copy the files (where in <code>dst</code> you pass the mounted directory of a bucket):</p>
<blockquote>
<pre class="lang-py prettyprint-override"><code>from shutil import copyfile
copyfile(src, dst) 
</code></pre>
<p>-- <em><a href="https://stackoverflow.com/questions/123198/how-can-a-file-be-copied">Stackoverflow.com: Questions: 123198: How can a file be copied</a></em></p>
</blockquote>
<hr />
<h3>Use the Python library to connect to the GCS bucket without <code>GCS Fuse</code></h3>
<p>As pointed by community member @anemyte, you can use the Cloud Storage client libraries to programmatically address your question:</p>
<ul>
<li><em><a href="https://cloud.google.com/storage/docs/reference/libraries" rel="nofollow noreferrer">Cloud.google.com: Storage: Docs: Reference: Libraries</a></em></li>
</ul>
<p>There is a Python code snippet that addresses the upload operation:</p>
<blockquote>
<pre class="lang-py prettyprint-override"><code>from google.cloud import storage


def upload_blob(bucket_name, source_file_name, destination_blob_name):
    &quot;&quot;&quot;Uploads a file to the bucket.&quot;&quot;&quot;
    # The ID of your GCS bucket
    # bucket_name = &quot;your-bucket-name&quot;
    # The path to your file to upload
    # source_file_name = &quot;local/path/to/file&quot;
    # The ID of your GCS object
    # destination_blob_name = &quot;storage-object-name&quot;

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print(
        &quot;File {} uploaded to {}.&quot;.format(
            source_file_name, destination_blob_name
        )
    )
</code></pre>
</blockquote>
<p>Please have in mind that you will need to have appropriate permissions to use the GCS bucket. You can read more about it by following below link:</p>
<ul>
<li><em><a href="https://cloud.google.com/storage/docs/uploading-objects#prereqs" rel="nofollow noreferrer">Cloud.google.com: Storage: Docs Uploading objects: Prerequisites</a></em></li>
</ul>
<blockquote>
<p>A side note!</p>
<p>You can also use <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity" rel="nofollow noreferrer">Workload Identity</a> as one of the ways to assign required permissions to your <code>Pod</code>.</p>
</blockquote>
<hr />
<h3>Additional resources:</h3>
<ul>
<li><em><a href="https://github.com/maciekrb/GCS-Fuse-sample" rel="nofollow noreferrer">Github.com: Maciekrb: Gcs fuse example</a></em></li>
<li><em><a href="https://cloud.google.com/storage/docs/gcs-fuse" rel="nofollow noreferrer">Cloud.google.com: Storage: Docs: GCS Fuse</a></em></li>
</ul>
<p>It passed my mind that you could want to use Python <strong>outside</strong> of the <code>Pod</code> (like from your laptop) to get the file copied from the <code>Pod</code> to <code>GCS bucket</code>. I'd reckon you could follow this example:</p>
<ul>
<li><em><a href="https://github.com/kubernetes-client/python/blob/master/examples/pod_exec.py" rel="nofollow noreferrer">Github.com: Kubernetes client: Python: Examples: Pod exec.py</a></em></li>
</ul>
