在Vue中从Google Cloud Storage提供用户图像
以下是我的项目的一些背景信息:
Here is some background info for my project:
- 带有Django Rest Framework的Django后端API,用于与前端通信
- 带有Vuetify的VueJS前端(SPA)
- 用户使用用户名和密码进行身份验证,我使用JWT身份验证api请求(python lib为
djangorestframework-simplejwt
) - 我计划将前端和API托管在同一Google App Engine上
- 该应用具有照片库。用户只能看到自己的照片。
- A Django backend API with Django Rest Framework to communicate with the frontend
- A VueJS frontend (SPA) with Vuetify
- User authenticates with username and password and I use JWT to authenticate api requests (python lib is
djangorestframework-simplejwt
) - I plan to host both frontend and API on the same Google App Engine
- The app has a photo gallery. User can only see its own photos.
现在,我不确定如何提供照片...我想Google Cloud Storage是一种解决方法
Now I am not sure how to serve the photos... I guess Google Cloud Storage is the way to go.
选项1
最初,我认为使用后端检索照片会更简单,因为它可以使用服务帐户简单地向Cloud Storage进行身份验证。但是我想通过串行器将照片从后端传递到前端会非常慢。可能有几百张照片。
Option 1 Initially I thought it would be simpler to retrieve the photos with the backend as it can simply authenticate to Cloud Storage with a service account. But I guess passing photos from backend to frontend through a serializer will be very slow. There can be a few hundred photos.
选项2
仅将url发送到前端(仅当前用户的url)可能更有效。然后,前端获取图像(例如< v-img:src = {{photo.url}}>
)。问题是客户端需要向Google进行身份验证才能具有对Cloud Storage的读取权限。此时我迷路了。
Option 2
It is probably far more efficient to only send the urls to the frontend (only the urls of current user). The frontend then gets images (e.g. <v-img :src={{ photo.url }}>
). The problem is the client needs to authenticate to Google to be able to have read access to Cloud Storage. At this point I am lost.
- 我应该使用签名的网址吗?
- 我应该使用如此处所述。但是据我了解,这意味着用户必须拥有一个Google帐户并同意条款等。
- 我可以拥有对象级别的权限,还是必须是存储桶级别的?
- Should I use signed url?
- Should I authenticate the client to Storage with OAuth 2.0 as described here. But as I understand it, it means user has to have a Google account and consent to terms, etc.
- Can I have object-level permissions or is it necessarily bucket-level?
感谢您提供的任何见解!
Thanks for any insight you may provide!
阅读您的用例后,我建议您使用签名URL。
Reading your use case I would recomend using Signed URLs.
根据官方文档:
A signed URL is a URL that provides limited permission and time to make a request.
Signed URLs contain authentication information in their query string, allowing users without credentials to perform specific actions on a resource