Skip to content

Commit 29698c9

Browse files
authored
Merge pull request #1706 from microbiomedata/issue-1612-gcs-images
Add API endpoints for handling submission-related image uploads
2 parents c89313e + 2fccdb9 commit 29698c9

File tree

24 files changed

+1532
-154
lines changed

24 files changed

+1532
-154
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ NMDC_API_JWT_SECRET=generateme
3838
# Change this value to "testing" to run tests outside tox
3939
NMDC_ENVIRONMENT=development
4040

41+
# == Cloud Storage ==
42+
NMDC_GCS_USE_FAKE=True
43+
# Get this from the Google Cloud Console; only needed if NMDC_GCS_USE_FAKE=False
44+
# NMDC_GCS_PROJECT_ID=changeme
45+
# Use `openssl rand -hex 4` to generate the random string once. Keep the `local_` prefix for local development.
46+
# NMDC_GCS_OBJECT_NAME_PREFIX=local_[random_string]
47+
48+
4149
# (Optional) Slack incoming webhook URL the ingester can use to post messages to Slack.
4250
# Reference: https://api.slack.com/messaging/webhooks#create_a_webhook
4351
# SLACK_WEBHOOK_URL_FOR_INGESTER=changeme

.github/workflows/server.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ jobs:
4343
python-version: [3.12]
4444

4545
steps:
46+
# Use docker run directly instead of the `services` key because otherwise it's not possible
47+
# to pass the `-scheme` flag.
48+
# See:
49+
# - https://github.com/fsouza/fake-gcs-server/issues/561
50+
# - https://github.com/orgs/community/discussions/26688
51+
- name: Start Fake GCS Server
52+
run: |
53+
docker run -d --name fake-gcs-server \
54+
-p 4443:4443 \
55+
fsouza/fake-gcs-server \
56+
-scheme http
4657
- uses: actions/checkout@v4
4758
- name: Set up Python ${{ matrix.python-version }}
4859
uses: actions/setup-python@v5
@@ -54,6 +65,9 @@ jobs:
5465
echo 'export NMDC_TESTING_DATABASE_URI="postgresql://nmdc:nmdc@localhost:5432/nmdc"' >> .env
5566
echo 'export NMDC_MONGO_USER="${{ secrets.NMDC_MONGO_USER }}"' >> .env
5667
echo 'export NMDC_MONGO_PASSWORD="${{ secrets.NMDC_MONGO_PASSWORD }}"' >> .env
68+
echo 'export NMDC_GCS_USE_FAKE="true"' >> .env
69+
echo 'export NMDC_GCS_OBJECT_NAME_PREFIX="testing"' >> .env
70+
echo 'export NMDC_GCS_FAKE_API_ENDPOINT="http://localhost:4443"' >> .env
5771
- name: Install dependencies
5872
run: |
5973
python -m pip install --upgrade pip

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ services:
5555
environment:
5656
- PGHOST=db
5757
- UVICORN_RELOAD=True
58+
- GOOGLE_APPLICATION_CREDENTIALS=/gcp/credentials.json
5859
networks:
5960
- public
6061
- default
@@ -63,6 +64,7 @@ services:
6364
volumes:
6465
- ./data/ingest:/data/ingest
6566
- ./nmdc_server:/app/nmdc_server
67+
- $HOME/.config/gcloud/application_default_credentials.json:/gcp/credentials.json
6668
# Use a TTY so colors are preserved in the log output
6769
tty: true
6870

@@ -90,9 +92,18 @@ services:
9092
ports:
9193
- "4008:4008"
9294

95+
storage:
96+
image: fsouza/fake-gcs-server
97+
ports:
98+
- "4443:4443"
99+
volumes:
100+
- storage-data:/storage
101+
command: "-scheme http -public-host localhost:4443 -filesystem-root /storage"
102+
93103
volumes:
94104
app-db-data:
95105
app-db-ingest:
106+
storage-data:
96107

97108
networks:
98109
public:

docs/development.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ NMDC_MONGO_USER=changeme
7272
NMDC_MONGO_PASSWORD=changeme
7373
```
7474

75+
### Google Cloud Storage
76+
77+
Google Cloud Storage (GCS) is used to store images associated with Submission Portal submissions. By default, local development uses a local mock GCS server. This is controlled by the `NMDC_GCS_USE_FAKE` variable in `.env`. If you want to use the real GCS server, set this variable to `false`.
78+
79+
Whether you use the real or fake GCS server, you will need to set up authentication. The recommended way to do this is to use Application Default Credentials (ADC) with service account impersonation. Service account impersonation is required for generating signed URLs for uploading/downloading images directly to/from GCS.
80+
81+
1. Ask a team member with the necessary GCS permissions to associate your Google Cloud account with the NMDC Google Cloud project and service account.
82+
2. Install the Google Cloud Command Line Interface (CLI) by following the instructions at https://cloud.google.com/sdk/docs/install.
83+
3. Run the following command to set up Application Default Credentials (ADC):
84+
```bash
85+
gcloud auth application-default login --impersonate-service-account <service account email will be provided by team member>
86+
```
87+
88+
You also must generate a local object name prefix. This prefix is used to differentiate which system uploaded to the shared GCS bucket. Local development systems should use the prefix `local_<random_suffix>`.
89+
90+
1. Generate a random suffix using the following command:
91+
```bash
92+
openssl rand -hex 4
93+
```
94+
2. Set the `NMDC_GCS_OBJECT_NAME_PREFIX` variable in `.env` to `local_<random_suffix>`.
95+
96+
```bash
97+
NMDC_GCS_OBJECT_NAME_PREFIX=local_1234abcd # replace 1234abcd with your random suffix
98+
```
99+
100+
75101
## Load production data
76102

77103
The `nmdc-server` CLI has a `load-db` subcommand which populates your local database using a nightly production backup. These backups are stored on NERSC. You must have NERSC credentials to use this subcommand.

0 commit comments

Comments
 (0)