I am trying to use Google Cloud Storage node.js (or Firebase Storage admin) to delete a specific file in a specific directory. Here is my attempt:
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({
projectId: projectId,
keyFilename: 'serviceAccountKey.json'
});
const bucket = storage.bucket("bucket");
const file = bucket.file('path1/path2/filename');
file.delete().then(function(data) {
const apiResponse = data[0];
console.log(apiResponse);
}).catch(error => {
console.log(error);
});
Because my file is located in the path bucket/path1/path2/filename
, it is not directly located inside the bucket. But when I run the codes, it produces an error:
{
message: 'No such object: bucket/path1/path2/filename',
domain: 'global',
reason: 'notFound'
}
I have also tried
const bucket = storage.bucket("bucket/path1/path2/");
const file = bucket.file('filename');
It failed with the same error.
I did take a look at the documentation . It did not mention anything about delete a file at a specific directory. In that case, how do I delete a specific file at a specific directory in my Google Cloud Storage with node.js then?
make sure that:
1- you are passing the correct bucket name
2- the full name of the file starting from it's first directory and it should be something like "read/test.txt"
3- the file indeed exists because you may have deleted and then tried again and this case the file won't exist
You may try also this code example:
function main(bucketName, filename) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function deleteFile() {
// Deletes the file from the bucket
await storage.bucket(bucketName).file(filename).delete();
console.log(`gs://${bucketName}/${filename} deleted.`);
}
deleteFile().catch(console.error);
// [END storage_delete_file]
}
main("my-bucket", "read/test.txt")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.