8

I use aws-android-sdk-1.4.3/samples/S3_SimpleDB_SNS_SQS_Demo to preview my files stored on Amazon (Amazon Simple Storage Service). Looking through code I saw that they use this, to acces the files:

com.amazonaws.demo.s3.S3.getDataForObject (line 130)

 public static String getDataForObject( String bucketName, String objectName ) {
        return read( getInstance().getObject( bucketName, objectName ).getObjectContent() );
    }


protected static String read( InputStream stream ) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream( 8196 );
        byte[] buffer = new byte[1024];
        int length = 0;
        while ( ( length = stream.read( buffer ) ) > 0 ) {
            baos.write( buffer, 0, length );
        }

        return baos.toString();
    }
    catch ( Exception exception ) {
        return exception.getMessage();

    }
}

}

Well, I have modified this methods to return ByteArrayOutputStream instead then I easily transform it to String or Bitmap (applying ByteArrayOutputStream.toByteArray() then using BitmapFactory.decodeByteArray(byte[] data, int offset, int length, Options opts)).

So, it works on text-files and pictures. My problem is when I try to access videos. So, my questions are:

1.Using the method provided above, how could I get a video from ByteArrayOutputStream (ByteArrayOutputStream.toString()) and play it in a VideoView or using MediaPlayer or an approach... ?

2 . Does anybody know any other solution to this problem of preview videos stored on Amazon ? (I heard that on their sdk for IOS they use URLs to access files...)

PS: Supplying the file URL and open it in browser does not make sense, because this URLs expire after a wile.

AlexAndro
  • 1,846
  • 1
  • 26
  • 51

3 Answers3

9

First we have to provide the name of our bucket and the object (see aws-android-sdk-1.4.3/samples/S3_SimpleDB_SNS_SQS_Demo for a complet guide) we want to open then get the URL to our object:

    AWSCredentials myCredentials = new BasicAWSCredentials("YOUR_AMAZON_ACCESS_KEY_ID", "YOUR_AMAZON_SECRET_KEY_ID");
    AmazonS3 s3client = new AmazonS3Client(myCredentials);
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
    URL objectURL = s3client.generatePresignedUrl(request);

Now, just play the video in a video view, supplying the URL obtained:

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    mediaCtrl = new MediaController(this);
    mediaCtrl.setMediaPlayer(videoView);
    videoView.setMediaController(mediaCtrl);
    Uri clip = Uri.parse(objectURL.toString());
    videoView.setVideoURI(clip);
    videoView.requestFocus();
    videoView.start();

I want to give thanks to @CommonsWare for

  • indicating me through REST API (even the code I used is from aws-sdk reading the REST API documentation helped me and show also other ways of requesting Amazon objects)

  • indicating me to use generatePresignedUrl()

  • the code for playing the video is also inspired from his materials.

AlexAndro
  • 1,846
  • 1
  • 26
  • 51
  • doesnt this throw MediaPlayer: setDataSource IOException happend ? beacause the url is not in the format which android player will accept – therealprashant Mar 26 '16 at 12:55
  • @therealprashant I don't remember at this time, was long time ago :). But once you have the url I think you can format it as per your needs. – AlexAndro Mar 28 '16 at 07:45
  • @AlexAndro Can u pls share which sdk u used ? Using https://github.com/aws/aws-sdk-android/tree/master/aws-android-sdk-s3 is not working – Erum Oct 25 '17 at 06:49
  • @Erum The version is specified `aws-android-sdk-1.4.3` Anyway this is realy old, I assume now there are other ways to use Amazon SDK, maybe using Android Studio Gradle – AlexAndro Oct 26 '17 at 12:28
3

1.Using the method provided above, how could I get a video from ByteArrayOutputStream (ByteArrayOutputStream.toString()) and play it in a VideoView or using MediaPlayer or an approach... ?

Maybe you could get it to work by publishing the byte array through a ContentProvider and openFile(). Here is a sample project where I demonstrate serving a file by means of a custom InputStream this way.

The media subsystem is rather fussy, though, and so I do not give you good odds on this working.

2 . Does anybody know any other solution to this problem of preview videos stored on Amazon ? (I heard that on their sdk for IOS they use URLs to access files...)

Last I checked, S3 had a REST API that you could use to generate URLs to the videos. I'd hand that URL to MediaPlayer or VideoView.

Supplying the file URL and open it in browser does not make sense, because this URLs expire after a wile.

But you control how long "a wile [sic]" is. Make it be 24 hours or something.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Thanks Mark, I will take a look to both solutions you provided. Do you know where can I take that "S3 REST API" from ?! About the duration of the URL availability, I'm afraid that it is controlled by Amazon.... – AlexAndro Nov 08 '12 at 11:47
  • @AlexAndro: "About the duration of the URL availability, I'm afraid that it is controlled by Amazon" -- I guarantee you that it is not. I control it myself, from the Ruby API, for making my books available for download to subscribers. Also, you can see the time-limited URLs in action in things like the S3 Explorer for Firefox. You are searcing for "pre-signed URLs" in the documentation IIRC. "Do you know where can I take that "S3 REST API" from" -- you don't need it, as the Java API has `generatePresignedUrl()` that you can use for this. – CommonsWare Nov 08 '12 at 11:56
0

@AlexAndro answer

AWSCredentials myCredentials = new BasicAWSCredentials("YOUR_AMAZON_ACCESS_KEY_ID", "YOUR_AMAZON_SECRET_KEY_ID");
AmazonS3 s3client = new AmazonS3Client(myCredentials);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName);
URL objectURL = s3client.generatePresignedUrl(request);

getWindow().setFormat(PixelFormat.TRANSLUCENT);
mediaCtrl = new MediaController(this);
mediaCtrl.setMediaPlayer(videoView);
videoView.setMediaController(mediaCtrl);
Uri clip = Uri.parse(objectURL.toString());
videoView.setVideoURI(clip);
videoView.requestFocus();
videoView.start();

solved my problem, but it needs to define your region using

s3client.setRegion(Region.EU_Paris.toAWSRegion())

EU_Paris is for eu-west-3

here you can find all regions

Mahmoud Mabrok
  • 865
  • 9
  • 17