Google cloud Storage ApiError: Required



上传文件到Bucket时出现错误。我尝试了所有的方法,但每次我得到这个错误。我的桶名和localalpath(文件路径)是正确的。我不知道这里需要什么。

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',
...
}

给出错误的代码如下:

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你看过文章谷歌云存储上传对象Nodejs

Google cloud 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);

我认为你的代码运行无序,也许可以尝试这样做

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));
}

谢谢@Justin Bender,我想没有比这更简单的了:

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"));

返回值:

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',

我在macos上尝试了Node 14、16和@google-cloud/storage@5.16.0、5.8.5和4.7.2版本。

然后我尝试通过REST上传文件(包括令牌生成),它工作了,所以桶和密钥文件都按预期工作。

深入研究gaxios库,我可以确认令牌生成工作,只有实际的上传请求由于某种原因失败。

我有同样的问题,也得到了一个json响应,而不是预期的缓冲区,当我试图download()一个文件。

我能解决这个问题的唯一方法是删除我的纱线。锁定并再次运行yarn以重新创建它。

相关内容

  • 没有找到相关文章

最新更新