Google Cloud HTTPS function (Error: There is an account problem for the requested project)

Viewed 311

UPDATE

It seems that I can't get the bucket's reference correctly or use the bucket functions.

When I do

// Get the photos' bucket
const bucket = gcs.bucket("photos");

console.log("deleting the bucket");

// Delete bucket
bucket.delete();

I get the error too (but the bucket exists in my project)

Error: There is an account problem for the requested project.
    at new ApiError (/workspace/node_modules/@google-cloud/common/build/src/util.js:58:15)
    at Util.parseHttpRespBody (/workspace/node_modules/@google-cloud/common/build/src/util.js:193:38)
    at Util.handleResp (/workspace/node_modules/@google-cloud/common/build/src/util.js:134:117)
    at retryRequest (/workspace/node_modules/@google-cloud/common/build/src/util.js:432:22)
    at onResponse (/workspace/node_modules/retry-request/index.js:206:7)
    at /workspace/node_modules/teeny-request/build/src/index.js:233:13
    at process._tickCallback (internal/process/next_tick.js:68:7)

PROBLEM

I am having this error:

{
  "error": {
    "code": 401,
    "message": "There is an account problem for the requested project.",
    "errors": [
      {
        "message": "There is an account problem for the requested project.",
        "domain": "global",
        "reason": "required",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}

 
PassThrough {  
   Unhandled error { Error: There is an account problem for the requested project
      at new ApiError (/workspace/node_modules/@google-cloud/common/build/src/util.js:58:15)
      _readableState:
          ReadableState { 
              at Util.parseHttpRespBody (/workspace/node_modules/@google-cloud/common/build/src/util.js:193:38) 
           ...

What I am doing is just:

  1. Get a bucket reference
  2. Get a file from the bucket with the file's path
  3. Get metadata and the signed url of the file.

My problem is the third step. When I call those functions, I don't receive any answer... no metadata, no url.

Here is the code:

const { Storage } = require("@google-cloud/storage");

const gsc = new Storage()

const serviceAccount = require("./serviceAccount.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://my_app_id.firebaseio.com",
  storageBucket: "my_app_id.appspot.com",
});


exports.validateImageDimensions = functions
  .region("us-central1")
  // Increased memory, decreased timeout (compared to defaults)
  .runWith({ memory: "2GB", timeoutSeconds: 120 })
  .https.onCall(async (data, context) => {
    // Get the image's owner
    const owner = context.auth.token.uid;

    // Get the image's info
    const { id, description, location, tags, time } = data;

    // Get the photos' bucket
    const bucket = gcs.bucket("photos");

    // Get the file's path
    const filePath = bucket.name + "/" + id;

    // Get the file
    const file = bucket.file(filePath);

    console.log(JSON.stringify(file));

    // Check if the file is a jpeg image
    const metadata = await file.getMetadata(); // <------- ERROR

    console.log(JSON.stringify(metadata));

    const isJpgImage = metadata[0].contentType === "image/jpeg";

    console.log(`Is jpeg? ${isJpgImage}`);

    // Get the file's signed urls
    const signedUrls = await file.getSignedUrl({
      action: "read",
      expires: "01-01-2100",
    });

    // signedUrls[0] contains the file's public URL
    const publicUrl = signedUrls[0];

    console.log(`publicUrl ${publicUrl}`);

    console.log(`Url: ${publicUrl}`);

The file exists in my storage... and when I console.log it I can see:

{
  "_eventsCount": 0,
  "_events": {},
  "id": "photos%2Fecbb4a5a-aa11-451f-b8af-efd3e8464a59",
  "baseUrl": "/o",
  "metadata": {},
  "acl": { "pathPrefix": "/acl", "owners": {}, "readers": {}, "writers": {} },
  "methods": {
    "exists": { "reqOpts": { "qs": {} } },
    "get": { "reqOpts": { "qs": {} } },
    "delete": { "reqOpts": { "qs": {} } },
    "getMetadata": { "reqOpts": { "qs": {} } },
    "setMetadata": { "reqOpts": { "qs": {} } }
  },
  "bucket": {
    "_eventsCount": 0,
    "baseUrl": "/b",
    "storage": {
      "baseUrl": "https://storage.googleapis.com/storage/v1",
      "projectId": "{{projectId}}",
      "authClient": {
        "cachedCredential": null,
        "scopes": [
          "https://www.googleapis.com/auth/iam",
          "https://www.googleapis.com/auth/cloud-platform",
          "https://www.googleapis.com/auth/devstorage.full_control"
        ],
        "jsonContent": null,
        "_cachedProjectId": null
      },
      "globalInterceptors": [],
      "acl": {
        "OWNER_ROLE": "OWNER",
        "READER_ROLE": "READER",
        "WRITER_ROLE": "WRITER"
      },
       ...

Any ideas? I have been googling for hours but no answer.

Also, my storage rules:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    function isSignedIn() {
        return request.auth.uid != null;
    }
    match /photos/{photo} {
      function hasValidSize() {
        // Max. photo size = 30MB (For all dimensions)
        return request.resource.size < 30 * 1024 * 1024;
      }
      function isImage() {
        return request.resource.contentType.matches("image/.*");
      }
      allow read: if true;
      allow write: if isSignedIn() && isImage() && hasValidSize();
    }
  }
}
0 Answers
Related