在文件系统和S3上存储图像以及轻松访问它们

huangapple go评论87阅读模式
英文:

Storing Images on File System vs S3 and accessing them easily

问题

我正在构建一个市场应用程序,并希望存储用户上传的图像。这些图像将可供任何非经过身份验证/经过身份验证的用户查看。似乎S3是我唯一找到的解决方案。但是,为页面上的所有图像生成预签名URL可能会产生大量的GET请求,特别是当用户在不同类别之间来回切换时。因此,我不确定这是否是切实可行的,或者是否需要缓存系统。我真的不想过于复杂化它,所以如果有更好的解决方案,我愿意尝试。

英文:

Im building a marketplace application and want to store user uploaded images. The images are going to be viewable by any non-authentcated/authenticated user. It seems like S3 is the only solution I could find. But having pre-signed urls for all the images on the page, would presumably be a lot of get request. Specially as a user goes back and forth between categories. So im not sure how realistic it would be or if I need a cache system. I really do not want to overcomplicate it so if there is a better solution im open to it.

答案1

得分: 0

缓存友好的预签名URL:

创建预签名URL是完全在客户端执行的操作,与之相关的费用或限制都不存在。因此,我不必担心实际URL的创建。

然而,为每次加载签名新的URL会使浏览器无法缓存您的图像,因为URL将始终更改。

为了解决这个问题,您可以确保在相同的到期时间内在一段时间内签名所有URL。这样,图像URL在一定时间内的请求之间将是相同的。例如,您可以始终将到期时间设置为接下来的15分钟,如下所示:

private String generateCacheFriendlyUrl(String fileName, HttpMethod httpMethod) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    // 添加5分钟以确保我们不会得到一个立即过期的URL
    calendar.add(Calendar.MINUTE, 5); 
    
    // 将时间四舍五入到下一个15分钟
    int unroundedMinutes = calendar.get(Calendar.MINUTE);
    int mod = unroundedMinutes % 15;
    calendar.add(Calendar.MINUTE, 15-mod);

    return amazonS3.generatePresignedUrl(s3BucketName, fileName, calendar.getTime(), httpMethod).toString();
}

这样,在URL具有相同到期时间的时间段内,浏览器就可以缓存图像。

英文:

Cache Friendly Pre-Signed URL:s

Creating a pre-signed url is a purely client-side operation and has neither a cost or limit associated to it. Hence, I would not be concerned about the actual creation of url:s.

However, signing new urls for every load will make browsers unable to cache your images because the url will always change.

To get around that you can make sure to sign all url:s during a set time-span with the same expiration time. That way the image url:s will be identical between requests for a set amount of time. For example you could always set the expiration to be the next following quarter hour, like this:

private String generateCacheFriendlyUrl(String fileName, HttpMethod httpMethod) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    // Add 5 minutes to make sure we don't get an immediately expiring url 
    calendar.add(Calendar.MINUTE, 5); 
    
    // Round time up to next quarter hour
    int unroundedMinutes = calendar.get(Calendar.MINUTE);
    int mod = unroundedMinutes % 15;
    calendar.add(Calendar.MINUTE, 15-mod);

    return amazonS3.generatePresignedUrl(s3BucketName, fileName, calendar.getTime(), httpMethod).toString();
}

That way images can be cached by browsers during the time-span where url:s have the same expiration time.

huangapple
  • 本文由 发表于 2023年7月13日 23:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定