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))
});
}
@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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With