Kirk Ross
Kirk Ross

Reputation: 7163

Google Cloud Storage - getting Not Found when trying to delete object that exists

This is likely a duh mistake but I can't figure this out.

I'm successfully uploading images to a bucket with a signed URL. When trying to delete the object from my Express backend, using the below code from Google's example, I get Not Found, yet the object is there with the correct name. Thoughts?

async function deleteFile(filename) {
  console.log(filename); // correct file name as exists in bucket
  try {
    await storage
      .bucket(bucketName) // correct bucket name and subfolder 'my-image-bucket/posts'
      .file(filename)
      .delete();
  } catch (e) {
    console.log('Error message = ', e.message); // Not Found
  }
}

Upvotes: 1

Views: 1358

Answers (1)

Brian Burton
Brian Burton

Reputation: 3842

The only red flag I'm seeing is, "correct bucket name and subfolder 'my-image-bucket/posts'" next to .bucket(). You should only be passing the bucket name to .bucket() and then the full path to .file().

    const bucketName = 'my-image-bucket';
    const filename = 'posts/image.jpg';
    await storage
      .bucket(bucketName)
      .file(filename)
      .delete();

Upvotes: 6

Related Questions