maximus383
maximus383

Reputation: 462

Cloud Functions: How to upload a folder that contains images?

I want to deploy a bot using Cloud Functions from Google Cloud Platform. My code is write in python, but I want to add a folder with a lot of images that my bot will tweet on twitter. I want to do that to make sure my bot will run 24/7, and add to Cloud Scheduler to schedule my tweets.

Is possible to upload the image folder there somehow?

I tried to upload all of my file in a .zip file and upload there, but it will fail to build. Alone just code is working fine. I use Google Developer Platform for some time but I never tried this.

Here are the errors from google cloud platform:

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 3,
      "message": "Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."
    },
    "authenticationInfo": {
      "principalEmail": "robertmaximus383@gmail.com"
    },
    "serviceName": "cloudfunctions.googleapis.com",
    "methodName": "google.cloud.functions.v1.CloudFunctionsService.CreateFunction",
    "resourceName": "projects/twitter-post-bot/locations/us-central1/functions/twitter-post"
  },
  "insertId": "-63htw4d2bfe",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "twitter-post-bot",
      "region": "us-central1",
      "function_name": "twitter-post"
    }
  },
  "timestamp": "2022-01-31T08:06:24.986674Z",
  "severity": "ERROR",
  "logName": "projects/twitter-post-bot/logs/cloudaudit.googleapis.com%2Factivity",
  "operation": {
    "id": "operations/dHdpdHRlci1wb3N0LWJvdC91cy1jZW50cmFsMS90d2l0dGVyLXBvc3QvZEt5N24yX1NjTE0",
    "producer": "cloudfunctions.googleapis.com",
    "last": true
  },
  "receiveTimestamp": "2022-01-31T08:06:25.259596536Z"
}

2

{
  "textPayload": "Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging\n",
  "insertId": "000000-5838e3b2-5d8d-4187-86ca-97327d016436",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "region": "us-central1",
      "function_name": "twitter-post",
      "project_id": "twitter-post-bot"
    }
  },
  "timestamp": "2022-01-31T08:06:24.930142467Z",
  "severity": "ERROR",
  "labels": {
    "execution_id": ""
  },
  "logName": "projects/twitter-post-bot/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "receiveTimestamp": "2022-01-31T08:06:25.532983237Z"
}

3

{
  "textPayload": "Traceback (most recent call last):\n  File \"/layers/google.python.pip/pip/bin/functions-framework\", line 8, in <module>\n    sys.exit(_cli())\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/click/core.py\", line 829, in __call__\n    return self.main(*args, **kwargs)\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/click/core.py\", line 782, in main\n    rv = self.invoke(ctx)\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/click/core.py\", line 1066, in invoke\n    return ctx.invoke(self.callback, **ctx.params)\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/click/core.py\", line 610, in invoke\n    return callback(*args, **kwargs)\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/_cli.py\", line 37, in _cli\n    app = create_app(target, source, signature_type)\n  File \"/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py\", line 288, in create_app\n    spec.loader.exec_module(source_module)\n  File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n  File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n  File \"/workspace/main.py\", line 2, in <module>\n    from twitter import *\nModuleNotFoundError: No module named 'twitter'",
  "insertId": "000000-438da477-ef7c-4c81-98a2-21bdccc7ca12",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "twitter-post-bot",
      "function_name": "twitter-post",
      "region": "us-central1"
    }
  },
  "timestamp": "2022-01-31T08:06:24.238Z",
  "severity": "ERROR",
  "labels": {
    "execution_id": ""
  },
  "logName": "projects/twitter-post-bot/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "receiveTimestamp": "2022-01-31T08:06:25.532983237Z"
}

Upvotes: 1

Views: 1447

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 76018

When you deploy your code on Cloud Functions, the code is taken, compiled and packaged in a container (with Buildpack) and deployed on Cloud Functions.

In your case, the code is not compiled, only packaged.

Your source code is kept in the container, but in a specific directory. I wrote an article with a sample in Golang but you can find where the statics files are stored: /workspace/src/


That will fix your issue, but I think you have a design issue. The static and unstructured data should be in your Cloud Functions. Cloud Storage is the perfect place for that, and it's a better and more scalable solution.

Upvotes: 1

Related Questions