[英]How to get last created file in a bucket using Google Cloud Storage Java SDK?
How do I get the last created file in a GCS bucket using Google Cloud Storage Java SDK?如何使用 Google Cloud Storage Java SDK 在 GCS 存储桶中获取最后创建的文件? Currently, I am using the
storage.list()
and iterating through all the files and thus getting the last created file.目前,我正在使用
storage.list()
并遍历所有文件,从而获取最后创建的文件。
Is there a better way of doing this?有没有更好的方法来做到这一点?
Cloud Storage doesn't offer flexible querying of objects. Cloud Storage 不提供灵活的对象查询。 If you want to find objects based on some property, such as upload time, you should store that information in a database whenever an object created, updated, or delete.
如果要根据某些属性(例如上传时间)查找对象,则应在创建、更新或删除对象时将该信息存储在数据库中。 Then, you can just query the database to find what you want.
然后,您可以查询数据库以找到您想要的内容。
You could write a cloud function which copies any uploaded file to a fixed key (listen on the finalize GCS event), say key 1. Then your application just queries key 1.您可以编写一个云函数,将任何上传的文件复制到一个固定的键(监听 finalize GCS 事件),比如键 1。然后您的应用程序只查询键 1。
Instead of copying the file you could also look at a peristent cache like Redis to store key 1.除了复制文件,您还可以查看像 Redis 这样的持久缓存来存储密钥 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.