How to cache images from S3 bucket with dynamic URL using Picasso?

Viewed 1091

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.

1 Answers

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.

Related