Angular Slingshot Delete

Hey guys,

Trying to implement Slingshot to my meteor-angular app, but not quite sure the ES6 way of importing Slingshot? Any help would be highly appreciated!

Assuming you’ve added the package

meteor add edgee:slingshot

you would import it as:

import { Slingshot } from 'meteor/edgee:slingshot'

Thanks for quick reply hwillson! Seems like importing it is not necessary, works without import… Is there an elegant solution to delete files from google cloud storage as slingshot only provides uploads…

If you are using Angular 2 with TypeScript, and if you do not find any typings files for Slingshot, you can just do declare const Slingshot: any; to get rid of warning.

Slingshot is only for uploading, you need use AWS sdk to delete:

  npm install --save aws-sdk

  npm install @types/aws-sdk --save-dev         // if you are using TypeScript 2.x
  typings install dt~aws-sdk --global --save    // if you are using TypeScript 1.x

Then

  import * as AWS from 'aws-sdk';

  AWS.config.update({
    accessKeyId: (YourAWSAccessKeyId,
    secretAccessKey: (YourAWSSecretAccessKey
  });

  const s3 = new AWS.S3();
  const params = {
    Bucket: 'xxx',
    Key: `${path}/${filename}`
  };

  s3.deleteObject(params, error => {
    if (error) console.log(error);
  });

Hi Hongbo_Miao, thank you for your reply! What would be the best package for google cloud delete option, as I’m using google cloud but not amazon s3?

Thank you!

Sorry, I didn’t use google cloud before, so I cannot recommend any packages. I guess some google cloud sdk?

For those who are interested in this solution please read bellow how to delete files from google cloud ->

    // BEFORE RUNNING:
    // ---------------
    // 1. If not already done, enable the Cloud Storage JSON API
    //    and check the quota for your project at
    //    https://console.developers.google.com/apis/api/storage
    // 2. This sample uses Application Default Credentials for authentication.
    //    If not already done, install the gcloud CLI from
    //    https://cloud.google.com/sdk/ and run
    //    'gcloud beta auth application-default login'
    // 3. Install the Node.js client library and Application Default Credentials
    //    library by running 'npm install googleapis --save'

    import { Meteor } from 'meteor/meteor';
    import { check } from 'meteor/check';
    import google from 'googleapis';

    Meteor.methods({
      'delete.style.image' (file, designId, imageId){
        check(file, String);
        check(designId, String);
        check(imageId, String);

        const storage = google.storage('v1');

        // Configure your object accordingly
        // my test app stores files in userId/style/filename.jpg
        let del = {
          org: Meteor.userId(),
          section: 'style',
          file: file
        }

        google.auth.getApplicationDefault((err, authClient) => {
          if (err) {
            console.log('Authentication failed because of ', err);
            return;
          }
          if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            let scopes = ['https://www.googleapis.com/auth/cloud-platform'];
            authClient = authClient.createScoped(scopes);
          }

          let request = {
            bucket: "your-bucket-name",
            object: encodeURIComponent(del.org + '/' + del.section + '/' + del.file),
            auth: authClient
          };

          storage.objects.delete(request, (err, result) => {
            if (err) {
              console.log(err);
            } else {
              console.log(result);
            }
          });
        });

        Collection.update({_id: designId}, {
          $pull: {
            'images': {
              _id: imageId
            }
          }
        })
      }
    });
1 Like