Community contributed extensions

S3Blobs Play Framework Module

The S3Blobs Play Framework Module provides an easy way to read an write files from Amazon S3 from within JPA entities.

The source code and issue tracker for S3Blobs is on github:
https://github.com/jamesward/S3-Blobs-module-for-Play

Installation

Install the module:

play install s3blobs

Configuration

Set these configuration parameters in your application.conf file to either the correct values or to environment variables:

s3.bucket=${S3_BUCKET}
aws.access.key=${AWS_ACCESS_KEY}
aws.secret.key=${AWS_SECRET_KEY}

Using S3Blobs

Add a S3Blob to an Entity:

package models;

import javax.persistence.Entity;

import play.db.jpa.Model;
import play.modules.s3blobs.S3Blob;

@Entity
public class Document extends Model
{
    public String fileName;
    public S3Blob file;
    public String comment;
}

Handle a file upload:

public static void doUpload(File file, String comment) throws FileNotFoundException
  {
    final Document doc = new Document();
    doc.fileName = file.getName();
    doc.comment = comment;
    doc.file = new S3Blob();
    doc.file.set(new FileInputStream(file), MimeTypes.getContentType(file.getName()));
    
    doc.save();
    listUploads();
  }

Handle a file download:

public static void downloadFile(long id)
  {
    final Document doc = Document.findById(id);
    notFoundIfNull(doc);
    response.setContentTypeIfNotSet(doc.file.type());
    renderBinary(doc.file.get(), doc.fileName);
  }

The full source code for this sample is available on github or in your project’s modules/s3blobs-0.1/samples-and-tests/plays3upload directory.