4

I am using Picasso to load the images from my private S3 bucket using dynamic URLs (Presigned URL). Problem is that every time I want to display the same image it will create a new URL and download it again instead of caching the same image and showing it.

Is there any way to cache the same image while using a different URL?

Example code:

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.HOUR, +6);
    Date date = calendar.getTime();

    URL url = s3.generatePresignedUrl(
            "my-bucket",
            "my-image.jpg,
            date
    );
    String urlString = url.toString();

    Picasso.get()
            .load(urlString)
            .into(imageView);

Dynamic URL example:

After 1st click: https: //s3.us-east-2.amazonaws.com/my-bucket/my-image.jpg?...&X-Amz-Signature=96dd696fdaf464fa42b2416f6261ba05e17d585578816e854e0a97a2782d177c

After 2nd click: https: //s3.us-east-2.amazonaws.com/my-bucket/my-image.jpg?...&X-Amz-Signature=8733d7fc9788759a851cf12fb1d1118584ca1f7cc33dc210b3fea4f762707b82

As you can see the first part of generated URL is always the same, the only thing that changes is the part after X-Amz-Signature.

The only reason I'm using pre-signed URLs is for better security. I want to have private access on my S3 bucket to avoid forced downloads from unknown sources, etc.

Miha M
  • 379
  • 1
  • 3
  • 14
  • 1
    Have you had a chance to read this? https://stackoverflow.com/a/23281195/2684 (not that I agree with that, but... I don't like Picasso). People can say all they want, every project where I used Picasso (a lot), is always less flexible when you want to do more things (like you here). – Martin Marconcini Jun 21 '19 at 18:51
  • Thank you for that. I also find out that the Glide library can help with my problem. I'll try it to see if it works. – Miha M Jun 22 '19 at 06:40
  • 2
    I managed to get it to work with Glide by using this class from Avinash Gupta: https://stackoverflow.com/questions/40550729/glide-image-cache-with-id-not-url – Miha M Jun 23 '19 at 08:45

1 Answers1

0

Why not use Glide, it will take care of the caching of memory and disk automatically. Its really nice and works smoothly.

You can know more by going to their official LINK.

abby
  • 350
  • 4
  • 21