Google cloud Storage ApiError: Required

Viewed 729

I am getting errors when uploading file to Bucket. I tried all methods but every time I am getting this error. My bucket name and loacalpath(File Path) are correct. I don't know what is required here.

ApiError: Required
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  code: 400,
  errors: [ { message: 'Required', domain: 'global', reason: 'required' } ],
  response: 
    ...
    allowHalfOpen: true,
    statusCode: 400,
    statusMessage: 'Bad Request',
    request: {
      agent: [Agent],
      headers: [Object],
      href: 'https://storage.googleapis.com/upload/storage/v1/b/demo-bucket-log0/o?uploadType=multipart&name=demo.log'
    },
    body: '{\n' +
      '  "error": {\n' +
      '    "code": 400,\n' +
      '    "message": "Required",\n' +
      '    "errors": [\n' +
      '      {\n' +
      '        "message": "Required",\n' +
      '        "domain": "global",\n' +
      '        "reason": "required"\n' +
      '      }\n' +
      '    ]\n' +
      '  }\n' +
      '}\n',
    ...
}

The code that gives the error is below.

function createBlob(bucketName, name, localFilePath) {
    return new Promise((resolve, reject) => {
        const bucket = gcpStorage.bucket(bucketName);
        bucket.upload(localFilePath).then(() => resolve("sucess")).catch((err) => reject(err))
    });
}
3 Answers

@Pruthvi Hingu Have you seen article Google Cloud Storage Upload Object Nodejs

Google Clouds Upload Docs

// The ID of your GCS bucket
const bucketName = 'your-unique-bucket-name';

// The path to your file to upload
const filePath = 'path/to/your/file';

// The new ID for your GCS file
const destFileName = 'your-new-file-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function uploadFile() {
  await storage.bucket(bucketName).upload(filePath, {
    destination: destFileName,
  });

  console.log(`${filePath} uploaded to ${bucketName}`);
}

uploadFile().catch(console.error);

I think your code is running out of order maybe try something like this

function createBlob(bucketName, name, localFilePath) {
    // creating promise
    return new Promise((resolve, reject) => {
        gcpStorage.bucket(bucketName).upload(localFilePath).then(data => {
                resolve("sucess", data);
            }).catch((error) => reject(error));
        }
    }).catch((err) => console.log(err));
}

Thanks @Justin Bender, I guess it doesn't get any simpler than that:

const { Storage } = require("@google-cloud/storage");
const storage = new Storage({keyFilename: "mykeyfile.json"});

async function uploadFile() {
  try {
    return await storage.bucket("mybucket").upload("test.txt", {
      destination: "test.txt",
    });
  } catch (err) {
    console.error(err);
  }
}

uploadFile().then(() => console.log("done")).catch((err) => console.error("error"));

This is what is returned:

ApiError: Required
    at new ApiError (/.../node_modules/@google-cloud/common/build/src/util.js:73:15)
    at Util.parseHttpRespBody (/.../node_modules/@google-cloud/common/build/src/util.js:208:38)
    at Util.handleResp (/.../node_modules/@google-cloud/common/build/src/util.js:149:117)
    at /.../node_modules/@google-cloud/common/build/src/util.js:479:22
    at onResponse (/.../node_modules/retry-request/index.js:228:7)
    at /.../node_modules/teeny-request/build/src/index.js:157:17
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
...
body: '{\n' +
      '  "error": {\n' +
      '    "code": 400,\n' +
      '    "message": "Required",\n' +
      '    "errors": [\n' +
      '      {\n' +
      '        "message": "Required",\n' +
      '        "domain": "global",\n' +
      '        "reason": "required"\n' +
      '      }\n' +
      '    ]\n' +
      '  }\n' +
      '}\n',

I'm on macos and tried Node 14, 16 and versions of @google-cloud/storage@5.16.0, 5.8.5 and 4.7.2. Only v4 works.

I then tried uploading the file via REST (including token generation) and it worked, so both the bucket and the keyfile work as expected.

Digging into the gaxios library I could confirm that token generation works, only the actual upload request fails for some reason.

I had the same problem and also got a json response instead of the expected Buffer when I tried to download() a file.

The only way I could solve it was to delete my yarn.lock and run yarn again to recreate it.

Related