I am currently trying to build a simple file (csv only) processing system in GCP, basically all it does is when a new file is uploaded, the code parses it and stores it in a database.
I created a new topic (file-upload) and a new service account with these commands (following these steps : https://cloud.google.com/run/docs/tutorials/pubsub#integrating-pubsub) :
gcloud projects add-iam-policy-binding my-project \
--member=serviceAccount:[email protected] \
--role=roles/iam.serviceAccountTokenCreator
gcloud iam service-accounts create cloud-run-pubsub-invoker \
--display-name "Cloud run PubSub Invoker"
gcloud run services add-iam-policy-binding myservice \
--member=serviceAccount:[email protected] \
--role=roles/run.invoker
Then created the subscription :
gcloud pubsub subscriptions create file-upload-sub --topic file-upload \
--push-endpoint=https://myservice-myurl.a.run.app/api/test/ \
--push-auth-service-account=cloud-run-pubsub-invoker@my-project.iam.gserviceaccount.com
I have a bucket, and I binded an "OBJECT_FINALIZE" event to it with the following command :
gsutil notification create -t file-upload -f json -e OBJECT_FINALIZE gs://MyBucket
My endpoint has the following routes (note : it's .net app within a docker image deployed on a cloud run) :
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
//GET : api/test
[HttpGet]
public IActionResult Get()
{
return Ok($"Get route pinged{Environment.NewLine}");
}
//POST : api/test
[HttpPost]
public IActionResult Post([FromBody] string bodyContent)
{
return Ok($"Content received : {bodyContent}{Environment.NewLine}");
}
}
When I send a request with curl with the command :
curl https://myservice-myurl.a.run.app/api/test/ -H "Authorization: Bearer $(gcloud auth print-identity-token)" -H "Content-Type: application/json" -d '"data"'
Everything is ok (I receive the confirmation in the console and in the log viewer) but when I try and push a message directly to the topic with gcloud pubsub topics publish file-upload --message '"test"'
, I only get 400 responses in the log viewer with no payload that explains to me where it breaks
EDIT : So apparently it was a problem linked to the way I tried to test the application, for some reason PubSub transforms the way messages are sent to cloud run when they're basic strings so it breaks .net content validation. I changed my code to accept the metadatas of a file in parameters and everything works fine
EDIT 2 : Wrote a proper answer to explain where I was wrong