NodeJS Google Storage unable to create new Bucket with gcloud utility

2.6k views Asked by At

So i am having some trouble with the gcloud plugin for Node.

I can create a bucket manually via their GUI and then use that container just fine to write files in my node program using the gcloud library provided by google.

The problem comes when i try to create a new bucket.

storage.createBucket('bucket1', function(err, bucket) {
                                      console.log(err)
                                      console.log(bucket)
                                    });

This always returns:

{ [Error: Invalid argument.]
  errors:
  [ { domain: 'global',
      reason: 'invalid',
      message: 'Invalid argument.' }],
   code: 400,
   message: 'Invalid argument.}

So i get that it thinks that i am passing an "invalid argument" having said that in this so helpful message 3 times ... yet the bucket name i am using is unique for my google storage. It follows the naming conventions to the best of my knowledge.

Any insights are most welcome.

Here is the string i use for setting up the link:

 storage = gcloud.storage({
              keyFilename: __dirname + '/../credentials/XXXXXXXX-b1e05a5128e9.json',
              projectId: 'XXXXXXXX'
            });

Note that i am not having any trouble reading and writing to a bucket that i created manually via the GUI. Just cannot create a new bucket.

I also tried with what should be a unique name globally across all of the cloud storage:

'com-missingmarble-bucket1'

Edit: Giving the API test as per steven's suggestion yields the same response. I authenticated the browser with oAuth ... a little different than the auth done in the node api but the result is the same. while it is possible that i did not format my request in the API tester correctly here is what i get:

Request:

POST https://www.googleapis.com/storage/v1/b?project=XXXXXXXXX&key={YOUR_API_KEY}
Content-Type:  application/json
Authorization:  Bearer xxxxxxxxxx
X-JavaScript-User-Agent:  Google APIs Explorer

{
 "name": "com-missingmarble-bucket1"
}

Response:

    400 Bad Request

    {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "invalid",
        "message": "Invalid argument."
       }
      ],
      "code": 400,
      "message": "Invalid argument."
     }
}
1

There are 1 answers

0
JJ Geewax On

Based on the comment above, it sounds like your issue is resolved...

For future people looking at this question, it sounds like the issue was you need to provide which project the bucket should belong to while creating it.

When you upload/download a file you specify a bucket, so Google doesn't need to know the project ID / number because the bucket is globally unique and tied to a single project.

On the flip side, when you're creating a bucket, it's possible that a single set of credentials has access to multiple projects, so you need to specify which project the bucket should belong to.