2

Hello all actually i am using google cloud platform and there i am storing my coupons images in gcs buckets. Now does google provide any api to delete an existing image from gcs buckets. I searched a lot on its docs google docs also have seen many blogs but what everyone do is deleting data from database but no one tell about how to delete image from buckets. If anyone have done this please help me it would be really appreciable.

Thanks

Community
  • 1
  • 1
Prakash Kumar
  • 829
  • 1
  • 15
  • 32

2 Answers2

4

Sure.

  1. Via console you can use gsutil command in this way. You just should install gsutil command.
  2. Via api rest you can use this service. You can try this api here.
  3. Also there are libraries for python, java or other languajes.
  4. From @MikeSchwartz suggestion. Using cloud console, you can manage your objects manually. Link.

Update 2: example on NodeJS

We can choose between three options. Using request module, Google cloud NodeJS client or Google API NodeJS client. But first of all, you should authorise your server to make request to Google Cloud Storage (GCS). To do that:

  • Open the Console Credentials page
  • If it's not already selected, select the project that you're creating credentials for.
  • Click Create credentials​ and choose Service Account Key.
  • In the dropdown select Compute Engine default service account. Then click create. A Json file will be downloaded.
  • In the left panel click Overview and type cloud storage in the finder.
  • Click Google Cloud Storage and make sure that this api is enabled.
  • Rename the downloaded json to keyfile.json and put it in accesible path for your NodeJS code.
  1. Google cloud NodeJS client. Here the official repository with a lot of samples.

    var fs = require('fs'); var gcloud = require('gcloud');

    var gcs = gcloud.storage({ projectId: 'your-project', keyFilename: '/path/to/keyfile.json' }); var bucket = gcs.bucket('your-bucket'); var file = bucket.file('your-file'); file.delete(function(err, apiResponse) {}):

  2. Using request module.

    npm install request

Then in your code:

var request = require('request');

request({
  url: 'https://www.googleapis.com/storage/v1/b/your-bucket/o/your-file',
  qs: {key: 'your-private-key'}, // you can find your private-key in your keyfile.json
  method: 'DELETE'
}, function(error, response, body){});
  1. Using Google API NodeJS: I don't know how to use this, but there a lot of examples here.
Nico
  • 858
  • 10
  • 20
  • A third option is using the Cloud Storage browser (https://pantheon.corp.google.com/storage/browser) – Mike Schwartz Jun 05 '16 at 19:50
  • @MikeSchwartz yes!. Warning you put your own storage url. – Nico Jun 05 '16 at 19:58
  • sorry if you got me wrong but actually i want to call the api inside my code which i am writing in nodejs. can you please tell me how can i do that ?? – Prakash Kumar Jun 05 '16 at 20:56
  • can you please tell me how can i do that in javascript as there java example is mentioned ?? also i know bucket name and the name of the pic i want to be deleted ?? – Prakash Kumar Jun 05 '16 at 21:05
  • @PrakashKumar you can take option 2. Use http request in your code. [Docs here](https://nodejs.org/api/http.html). I can write an example when I comeback to my house. – Nico Jun 05 '16 at 21:12
  • that would be really aprreciable thanks a lot for helping me out of here bro :) – Prakash Kumar Jun 05 '16 at 21:14
  • If you're using node.js, there's also the [gcloud-node](https://github.com/GoogleCloudPlatform/gcloud-node) library for dealing with Google APIs. That makes deleting objects much easier. – Brandon Yarbrough Jun 05 '16 at 22:19
  • also can you please tell me in order to show my pics in my android app do i have to tick that share on publlic url or not ?? also if i have to then how can i do that in my code because i don't think i should manually tick every image i add on gcs for sharing on public url ?? – Prakash Kumar Jun 05 '16 at 22:46
  • @PrakashKumar I've updated the post with examples. To define public read all objects in a bucket check [this](http://stackoverflow.com/questions/14371376/how-do-you-make-many-files-public-in-google-cloud-storage). Please don't forget mark this question as accept for my answer. Thanks. – Nico Jun 06 '16 at 03:10
1

Assuming you have your image file public url, you can do it like this

    import {Storage} from "@google-cloud/storage";
     const storage = new Storage({
        projectId: GCLOUD_PROJECT,
        keyFilename: 'keyfile.json'
   });

   const bucket = storage.bucket(GCLOUD_BUCKET);
   //var image_file="https://storage.googleapis.com/{bucketname}/parentfolder/childfolder/filename

var image_file="https://storage.googleapis.com/1533406597315/5be45c0b8c4ccd001b3567e9/1542186701528/depositphotos_173658708-stock-photo-hotel-room.jpg";

    new Promise((resolve, reject) => {
    var imageurl = image_file.split("/");
    imageurl = imageurl.slice(4, imageurl.length + 1).join("/");

    //imageurl=parentfolder/childfolder/filename
    storage
        .bucket(GCLOUD_BUCKET)
        .file(imageurl)
        .delete()
        .then((image) => {
            resolve(image)
        })
        .catch((e) => {
            reject(e)
        });

});

check on googles official documentation under code samples in this link https://cloud.google.com/storage/docs/deleting-objects or github https://github.com/googleapis/nodejs-storage/blob/master/samples/files.js