Delete old Slingshot uploaded images?

Im using Slingshot to upload images to an S3 bucket so users can set profile pictures. If they set a new profile picture the old one won’t be used again. How should I delete this old image?

I can think of 2 ways.

1: Use Slingshot to tell Amazon to delete the image when a new profile image is set.

2: Have a cron job with Amazon. As the image filenames are set to userID-date.jpg presumably I could look for duplicate userID’s and then delete the older time?

Has anyone done either of these or tried a different approach?

What happens until then? Is a random picture selected each time the user gets to see the image?

Slingshot gives me the url for the new image which I save to the database. So my app will show the new image.

Currently im not saving the old image url so there isnt really a way to ever see the old image again, which is why I might as well delete it.

Okay :slight_smile:
I used this code in a project. If I recall correctly, there was a problem with result not throwing errors if for example the Key didnt exsist. But I am not sure …

import { AWS } from 'meteor/peerlibrary:aws-sdk';
...
if (!Meteor.settings.AWSAccessKeyId || !Meteor.settings.AWSSecretAccessKey || !Meteor.settings.AWSRegion) {
        throw new Meteor.Error('internal-error', `Internal Server Error.`);      
      }

      AWS.config.update({
        region: Meteor.settings.AWSRegion,
        accessKeyId: Meteor.settings.AWSAccessKeyId,
        secretAccessKey: Meteor.settings.AWSSecretAccessKey,
      })

      const s3 = new AWS.S3();

      let aURL = this.downloadUrl.split("/"); // This would be your URL
      const key = aURL.splice(aURL.length - 2, aURL.length).join("/");

      const result = s3.deleteObjectSync({
        Bucket: `${bucketname}`, // You have to specify it somewhere
        Key: `${key}`
      });
...
1 Like