makeporngreatagain.pro
yeahporn.top
hd xxx

Build a Serverless App with Cloud Run that Creates PDF Files

1,455

Enable the Cloud Run API

  1. Open the navigation menu and select APIs & Services > Library. Then in the search bar, enter in “Cloud Run” and select the Cloud Run API from the results list.
  2. Click Enable and then hit the back button in your browser twice. Your Console should now resemble the following:
    cloud-run-api.png

Trigger your Cloud Run service when a new file is uploaded

  1. Run the following command to create a bucket in Cloud Storage for the uploaded docs:
    gsutil mb gs://$GOOGLE_CLOUD_PROJECT-upload
  2. And another bucker for the processed PDFs:
    gsutil mb gs://$GOOGLE_CLOUD_PROJECT-processed
  3. In Cloud Shell run the following command to tell Cloud Storage to send a Pub/Sub notification whenever a new file has finished uploading to the docs bucket:
    gsutil notification create -t new-doc -f json -e OBJECT_FINALIZE gs://$GOOGLE_CLOUD_PROJECT-upload

    The notifications will be labeled with the topic “new-doc”.

Deploy a simple Cloud Run service

  1. Open a new Cloud Shell session and run the following command to clone the Pet Theory repository:
    git clone https://github.com/webmagicinformatica/Build-a-Serverless-App-with-Cloud-Run-that-Creates-PDF-Files.git
  2. Then change your current working directory
    cd Build-a-Serverless-App-with-Cloud-Run-that-Creates-PDF-Files
  3. Now run the following commands in Cloud Shell to install the packages that your conversion script will be using:
    npm install express
    npm install body-parser
    npm install child_process
    npm install @google-cloud/storage
  4. To build and deploy the REST API, use Google Cloud Build. Run this command to start the build process
    gcloud builds submit  --tag gcr.io/$GOOGLE_CLOUD_PROJECT/pdf-converter
  5. The command builds a container with your code and puts it in the Container Registry of your project.
  6. Return to the Cloud Console, open the navigation menu, and select Container Registry > Images. You should see your container hosted:
    194f15ae0f52135f.png
  7. Return to your code editor tab and in Cloud Shell run the following command to deploy your application:
    gcloud beta run deploy pdf-converter \
      --image gcr.io/$GOOGLE_CLOUD_PROJECT/pdf-converter \
      --platform managed \
      --region us-central1 \
      --memory=2Gi \
      --no-allow-unauthenticated \
      --set-env-vars PDF_BUCKET=$GOOGLE_CLOUD_PROJECT-processed
  8. Create the environment variable $SERVICE_URL for the app so you can easily access it:
    SERVICE_URL=$(gcloud beta run services describe pdf-converter \
      --platform managed --region us-central1 --format="value(status.url)")
    
    echo $SERVICE_URL
  9. Make an anonymous POST request to your new service:
    curl -X POST $SERVICE_URL

    This will result in an error message saying “Your client does not have permission to get the URL”. This is good; you don’t want the service to be callable by anonymous users.

  10. Now try invoking the service as an authorized user:
    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" $SERVICE_URL

    If you get the response “OK” you have successfully deployed a Cloud Run service. Well done!

  11. Then create a new service account which Pub/Sub will use to trigger the Cloud Run services:
    gcloud iam service-accounts create pubsub-cloud-run-invoker --display-name "PubSub Cloud Run Invoker"
  12. Give the new service account permission to invoke the PDF converter service:
    gcloud beta run services add-iam-policy-binding pdf-converter --member=serviceAccount:pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com --role=roles/run.invoker --platform managed --region us-central1
  13. Find your project number by running this command:
    gcloud projects list
  14. Create a PROJECT_NUMBER environment variable, replacing [project number] with the Project Number from the last command:
    PROJECT_NUMBER=[project number]
  15. Then enable your project to create Cloud Pub/Sub authentication tokens:
    gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT --member=serviceAccount:service-$PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com --role=roles/iam.serviceAccountTokenCreator
  16. Finally, create a Pub/Sub subscription so that the PDF converter can run whenever a message is published on the topic “new-doc”.
    gcloud beta pubsub subscriptions create pdf-conv-sub --topic new-doc --push-endpoint=$SERVICE_URL --push-auth-service-account=pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com

See if the Cloud Run service is triggered when files are uploaded to Cloud Storage

  1. Copy some test files into your upload bucket:
    gsutil -m cp gs://spls/gsp644/* gs://$GOOGLE_CLOUD_PROJECT-upload
  2. Once the upload is done, return to your Cloud Console tab, open the navigation menu, and select Logging from under the Operations section.
  3. In the first dropdown, filter your results to Cloud Run Revision and click Add. Then click Run Query.
  4. In the Query results, look for a log entry that starts with file: and click it. It shows a dump of the file data that Pub/Sub sends to your Cloud Run service when a new file is uploaded.
  5. Can you find the name of the file you uploaded in this object?
  6. You can also go to the Processed Bucket to check PDF generated

 

Comments are closed, but trackbacks and pingbacks are open.

baseofporn.com https://www.opoptube.com
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.