Creating a Local Environment to Operate GCS Emulator from Flask

When creating an application that manipulates GCS (Google Cloud Storage) through Flask on Cloud Run, I wanted to use a GCS emulator in the local environment. This document describes how to set up such an environment.

Emulator to Use

Sample code

I created sample code in the following repository. By following the instructions in README.md, you can use the GCS emulator in a local environment to upload, download, and delete files.

Since you can start everything simply by cloning the repository and running make up, setup is straightforward.

Contents of docker-compose.yml

To use the GCS emulator in the Flask container, the STORAGE_EMULATOR_HOST environment variable is set:

<span>services</span><span>:</span>
<span>app</span><span>:</span>
<span>environment</span><span>:</span>
<span>-</span> <span>STORAGE_EMULATOR_HOST=http://gcs:4443</span>
<span>services</span><span>:</span>
  <span>app</span><span>:</span>
    <span>environment</span><span>:</span>
      <span>-</span> <span>STORAGE_EMULATOR_HOST=http://gcs:4443</span>
services: app: environment: - STORAGE_EMULATOR_HOST=http://gcs:4443

Enter fullscreen mode Exit fullscreen mode

Configuring Flask’s StorageClient

The STORAGE_EMULATOR_HOST environment variable is checked to determine whether to use the emulator:

<span>def</span> <span>get_storage_client</span><span>():</span>
<span>emulator_host</span> <span>=</span> <span>Config</span><span>.</span><span>STORAGE_EMULATOR_HOST</span>
<span>if</span> <span>emulator_host</span><span>:</span>
<span>client</span> <span>=</span> <span>storage</span><span>.</span><span>Client</span><span>(</span>
<span>credentials</span><span>=</span><span>AnonymousCredentials</span><span>(),</span>
<span>project</span><span>=</span><span>"</span><span>test</span><span>"</span><span>,</span>
<span>)</span>
<span>else</span><span>:</span>
<span>client</span> <span>=</span> <span>storage</span><span>.</span><span>Client</span><span>()</span>
<span>return</span> <span>client</span>
<span>def</span> <span>get_storage_client</span><span>():</span>
    <span>emulator_host</span> <span>=</span> <span>Config</span><span>.</span><span>STORAGE_EMULATOR_HOST</span>

    <span>if</span> <span>emulator_host</span><span>:</span>
        <span>client</span> <span>=</span> <span>storage</span><span>.</span><span>Client</span><span>(</span>
            <span>credentials</span><span>=</span><span>AnonymousCredentials</span><span>(),</span>
            <span>project</span><span>=</span><span>"</span><span>test</span><span>"</span><span>,</span>
        <span>)</span>
    <span>else</span><span>:</span>
        <span>client</span> <span>=</span> <span>storage</span><span>.</span><span>Client</span><span>()</span>

    <span>return</span> <span>client</span>
def get_storage_client(): emulator_host = Config.STORAGE_EMULATOR_HOST if emulator_host: client = storage.Client( credentials=AnonymousCredentials(), project="test", ) else: client = storage.Client() return client

Enter fullscreen mode Exit fullscreen mode

Conclusiuon

When building an application on Cloud Run that manipulates GCS through Flask, you might want to use a GCS emulator in your local environment. These are the setup steps. Note that in production you will not use the GCS emulator, so simply do not set the STORAGE_EMULATOR_HOST environment variable, specify your BUCKET_NAME, and grant the appropriate permissions to Cloud Run.

原文链接:Creating a Local Environment to Operate GCS Emulator from Flask

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
Remember happiness doesn't depend upon who you are or what you have; it depends solely on what you think.
幸福不在于你是谁,你拥有什么,而仅仅在于你自己怎么看待
评论 抢沙发

请登录后发表评论

    暂无评论内容