kaliber-scala / play-s3

S3 module for Play
MIT License
119 stars 49 forks source link

Can't stream S3 video/Image while downloading #66

Open hubino-testing2 opened 8 years ago

hubino-testing2 commented 8 years ago

I can't stream the S3 video/Image while downloading. I can able to read the private bucket by using my aws.accessKeyId and aws.secretKey. I have used the below way.

Code snippet: In HTML:

<!DOCTYPE html>
<html>
    <body>
        <img width="240" height="320" src="@routes.Application.loadS3File("user.png")" />
        <video controls="controls" preload controls width="320" height="240" name="Video Name">
            <source src="@routes.Application.loadS3File("user.mov")" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
        </video>
    </body>
</html>

In Controller:

**method-1:**
def loadS3File(filename: String) = Action { implicit request =>
        try {
            val bucket = S3("user")
            val result = bucket.get(filename)
            val file = Await.result(result, 60 seconds)
            val BucketFile(name, contentType, content, acl, headers) = file
            Ok.chunked(Enumerator(content)).as(contentType);
        }
        catch {
            case e: Exception =>
                BadRequest("Error: " + e.getMessage)
        }
    }

**method-2:**
def loadS3File(filename: String) = Action.async {
        val bucket = S3("user")
        bucket.get(filename).map {
            case BucketFile(name, contentType, content, acl, headers) =>
                val byteString: ByteString = ByteString.fromArray(content)
                Ok.chunked(Enumerator(byteString)).as(contentType)
        }

    }

It waits to read the whole file content from S3. But the it doesn't return video to html. If I tried to show the S3 image, it read the whole image content from S3 and return to the html successfully. I can't stream the video/Image while downloading in progress.

EECOLOR commented 8 years ago

The library does not support streaming out-of-the box. You might want to check out this thread on stackoverflow to get a feel of the complexity. You can also check #42

As for now I recommend you use the library to create an url directly to the file and use that with your videoplayer. This has the added benefit that the file does not need to travel through your server but goes directly from s3 to the browser of the client.