在 Heroku 上的 Play 框架中处理文件上传

发布于 2024-12-02 20:08:50 字数 560 浏览 1 评论 0原文

可能的重复:
在 Heroku(或类似的云服务)中存储 Blob

我意识到Heroku 有一个只读文件系统,并且研究了有关 attachment_fu 和其他类似的 Ruby gem 的问题,用于以暂时的方式处理文件上传,这样它们就不会真正到达磁盘(除了temp)直到它们被写入 Amazon S3。但是,我想在我即将进行的项目中使用 Play Framework (Java),并且需要支持将图片上传到图库。

显然,我需要使用像 S3 或数据库 blob 字段这样的可写后备存储:只要它适用于低容量网站(每月数百个请求),我就不会选择其中任何一个。有人用 Heroku 上的 Play 做过这个吗?我是 Play Framework 的新手,也是 Heroku 的新手,所以即使是非常明显的答案也会有所帮助!

Possible Duplicate:
Store Blob in Heroku (or similar cloud services)

I realize that Heroku has a read-only file system, and have looked at SO questions about attachment_fu and other similar Ruby gems for handling file uploads in a transitory way so that they never really hit disk (other than temp) until they get written to Amazon S3. However, I'd like to use the Play Framework (Java) for my upcoming project, and need to support uploading pictures to a gallery.

Obviously I'll need to use a writable backing store like S3 or a database blob field: I'm not married to either one as long as it works for a low volume web site (several hundred requests per month). Has anyone done this with Play on Heroku? I'm new to Play Framework, and new to Heroku, so even really obvious answers would help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-12-09 20:08:50

我在 github 上提供了如何使用 Amazon S3 执行此操作的示例:
https://github.com/jamesward/plays3upload

基本上你只需要将文件发送到S3并保存实体中的键:

AWSCredentials awsCredentials = new BasicAWSCredentials(System.getenv("AWS_ACCESS_KEY"), System.getenv("AWS_SECRET_KEY"));
AmazonS3 s3Client = new AmazonS3Client(awsCredentials);
s3Client.createBucket(BUCKET_NAME);
String s3Key = UUID.randomUUID().toString();
s3Client.putObject(BUCKET_NAME, s3Key, attachment);
Document doc = new Document(comment, s3Key, attachment.getName());
doc.save();
listUploads();

I put an example of how to do this with Amazon S3 on github:
https://github.com/jamesward/plays3upload

Basically you just need to send the file to S3 and save the key in the entity:

AWSCredentials awsCredentials = new BasicAWSCredentials(System.getenv("AWS_ACCESS_KEY"), System.getenv("AWS_SECRET_KEY"));
AmazonS3 s3Client = new AmazonS3Client(awsCredentials);
s3Client.createBucket(BUCKET_NAME);
String s3Key = UUID.randomUUID().toString();
s3Client.putObject(BUCKET_NAME, s3Key, attachment);
Document doc = new Document(comment, s3Key, attachment.getName());
doc.save();
listUploads();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文