How to load image from aws with picasso with private access

后端 未结 4 1003
广开言路
广开言路 2021-02-09 00:11

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

4条回答
  •  遥遥无期
    2021-02-09 00:33

    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 {
        // 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);
    

提交回复
热议问题