How to load image from aws with picasso with private access

Viewed 1516

I'm trying to load image stored on aws S3 into my android app using Picasso but I am getting a blank image with no errors in my logcat and nothing to me from general debugging around the relevant lines of code. We are having private access on images so image url can't work on browser. i need to display image into my android app using Picasso. but it doesn't work.

My code snippet below

  new Picasso.Builder(getApplicationContext()).downloader(new S3Downloader(getApplicationContext(), s3Client, bucket))
                .build()
                .load("https://s3-ea-east-8.amazonaws.com/music/MusicApp_3.jpg")
                .placeholder(R.drawable.img_placeholder)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(imageView);

By using above code image is displaying only very first time after installing app. next time its only showing placeholder image

I am using this library for displaying image.

The problem isn't with Picasso, it's with loading an image from a "private" url.

please suggest solutions

4 Answers

You need to generate a presigned Url from S3 client and you can pass that url to picasso. That url will be public and will have an expriy date.

You can use the below set of code , which is a aynctask which will use glide to attach private images from s3 by generating presignedurl.

  public class sets3imageasync extends AsyncTask<Object, Void, String> {
    // The list of objects we find in the S3 bucket

static final String s3bucket=bucket_name;


Context scontext;
ImageView image;


@Override
protected String doInBackground(Object... objects) {
    // Queries files in the bucket from S3.
    Calendar cal = Calendar.getInstance();
    Log.d(TAG, "doInBackground: in progress");
    cal.setTime(new Date());
    cal.add(Calendar.HOUR, +1);
    Date oneHourLater = cal.getTime();
    AmazonS3Client s3 =  (AmazonS3Client) objects[0];
    scontext= (Context) objects[1];
    image=(ImageView) objects[2];
    String imagekey=(String) objects[3]; 
    GeneratePresignedUrlRequest generatePresignedUrlRequest =
            new GeneratePresignedUrlRequest(s3bucket, imagekey)
                    .withMethod(HttpMethod.GET)
                    .withExpiration(oneHourLater);
    URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);

    Log.e("url was", url.toString());
    return url.toString();
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);


    Log.d(TAG, "onPostExecute: completed "+s);

    Glide.with(scontext).load(s)
            .apply(new RequestOptions()
                    .centerCrop())
            .placeholder(scontext.getResources().getDrawable(R.drawable.progress_animation))
            .into((image));

}

this can be called as below

new sets3imageasync().execute(s3Client,context,Image_view);

Simply use this:

Picasso.with(getApplicationContext()).load(your_url).noFade().into(imageView);
write below code to load image in Picasso. 
variables:-  
String file_path                          -->> this is your image file path 
Imageview mViewHolder.img_post_photo      -->> this is your imageview to load image.
                        Picasso.with(context)
                                .load(file_path)
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.mipmap.ic_launcher)
                                .into(mViewHolder.img_post_photo, new Callback() {
                                    @Override
                                    public void onSuccess() {

                                    }

                                    @Override
                                    public void onError() {
                                        Picasso.with(context)
                                                .load(file_path)
                                                .placeholder(R.mipmap.ic_launcher)
                                                .error(R.mipmap.ic_launcher)
                                                .into(mViewHolder.img_post_photo);
                                    }
                                });
Set dependencies in your app build.gradle file:-
compile 'com.squareup.picasso:picasso:2.5.2'

hope this code helps you.
Related