Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google cloud Storage ApiError: Required

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))
    });
}
like image 269
Pruthvi Hingu Avatar asked Apr 17 '26 13:04

Pruthvi Hingu


1 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));
}
like image 176
Cardano Goat Avatar answered Apr 20 '26 02:04

Cardano Goat