S3 Presigned URL generation in android studio using import software.amazon.awssdk.services.s3.S3Client

0

From Android Studio i am trying to create a presigned URL for a specific S3 video object using its Object Key, and then use that presigned URL to display the video within the app using a videoView, and also download it to the phone. Im not sure what this code would would like. Could you please provide a code snippet to get me in the right direction here? Again im using using import software.amazon.awssdk.services.s3.S3Client.

asked 14 days ago63 views
1 Answer
0

To generate a presigned URL for an S3 object in Android Studio using the AWS SDK for Java, you'll need to use the S3Presigner class along with the S3Client. Here's a code snippet to help you get started:

First, make sure you have the necessary dependencies in your build.gradle file:

implementation 'software.amazon.awssdk:s3:2.x.x'
implementation 'software.amazon.awssdk:s3-presigner:2.x.x'

Now, here's a code snippet to generate a presigned URL:

import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;

import java.time.Duration;

public String generatePresignedUrl(String bucketName, String objectKey) {
    try (S3Presigner presigner = S3Presigner.create()) {
        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .build();

        GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
                .signatureDuration(Duration.ofMinutes(10))
                .getObjectRequest(getObjectRequest)
                .build();

        PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(getObjectPresignRequest);
        
        return presignedRequest.url().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

This method generates a presigned URL that's valid for 10 minutes. You can adjust the duration as needed.

To use this presigned URL with a VideoView in your Android app:

String presignedUrl = generatePresignedUrl("your-bucket-name", "your-object-key");
VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoPath(presignedUrl);
videoView.start();

To download the file using the presigned URL:

private void downloadFile(String presignedUrl, String fileName) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL url = new URL(presignedUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                
                File file = new File(getExternalFilesDir(null), fileName);
                FileOutputStream outputStream = new FileOutputStream(file);
                
                InputStream inputStream = connection.getInputStream();
                byte[] buffer = new byte[4096];
                int bytesRead;
                
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
}

Remember to handle permissions for internet access and file writing in your AndroidManifest.xml file. Also, ensure you're not performing network operations on the main thread to avoid ANR (Application Not Responding) errors.
Sources
Create a presigned URL for Amazon S3 directory buckets to get an object using an AWS SDK - Amazon Simple Storage Service
Create a presigned URL for Amazon S3 directory buckets to get an object using an AWS SDK - AWS SDK Code Examples
Amazon S3 examples using SDK for JavaScript (v3) - AWS SDK Code Examples

profile picture
answered 14 days ago
AWS
SUPPORT ENGINEER
reviewed 12 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions