建立一个使用 GCS 或 AWS S3 的 Python 文档管理系统的最佳方法是什么?

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

What is the best way to build a Document Management System in python using GCS or AWS S3?

问题

我正在构建一个工具,其中特定账户/租户的用户可以上传图片/视频(创建/删除),还可以创建/删除文件夹来组织这些图片。这些图片/视频可以稍后拖放到页面上。这个页面将对该账户中的每个人都可访问。所以我考虑了2种架构流程,但都似乎存在权衡。

  1. 我想可以为文档管理系统中可用的每个资源生成已签名的URL,以及用于页面中使用的每个资源。如果页面中使用的图片较少,这种方法有效。但如果用户在页面中有30-40张图片,客户端必须在每次用户加载页面时请求每个资源的已签名URL。这会增加客户端渲染页面时的延迟。

  2. 另一种架构是将所有上传的资源放在一个公共桶中(明确告知用户所有上传的资源都将是公共的)。明显的权衡是安全性。

是否有一种方式可以安全地允许用户拥有大量资源?类似于为路径生成已签名URL,而不是为blob本身生成已签名URL,是否可能?例如,是否可以为/folder1/folder2生成已签名URL,以便客户端可以请求文件夹2中的所有blob而无需多次请求服务器?

我想要实现的是在不牺牲安全性的前提下尽量减少延迟。

英文:

I'm building a tool where users in a particular account/tenant can upload images/videos (CREATE/DELETE) and also create/delete folders to organize those images. These images/videos can later be dragged and dropped onto a page. This page will be accessible to everyone in that account. So I have thought of 2 architecture flows but both seem to have trade-offs.

  1. I thought I can generate signed url for each of the resource available in the document management system and for each resource that is used in the page. This method works if there are less number of images used in a page. What if the user has 30-40 images in a page, the client has to request signed URLs for each of those resource everytime a user loads the page. This increases latency while rendering the page on the client side.

  2. Another architecture is to put all of the uploaded resource in a public bucket (explicitly stating the user that all uploaded resource will be public). The obvious tradeoff is security.

Is there a way where I can securely allow users to have numerous resources? Something like instead of generating a signedURL for the blob itself, would it be possible to generate a signedURL for a path? Example: instead of generating a signed url for /folder1/folder2/blob.png would I be able to generate a signedURL for /folder1/folder2 so that the client can request for all the blobs within the folder2 without multiple requests to the server?

What I want to achieve is minimal latency without compromising security.

答案1

得分: 1

以下是翻译好的部分:

For Google Cloud Storage you can use python and then use Flask as your web framework and then use this code to upload documents:
要使用Google Cloud Storage,您可以使用Python,并使用Flask作为您的Web框架,然后使用以下代码上传文档:

Your index should look like this
您的索引应该如下所示:

<!doctype html>
<html>
  <head>
    <title>File Upload</title>
  </head>
  <body>
    <h1>File Upload</h1>
    <form method="POST" action="" enctype="multipart/form-data">
      <p><input type="file" name="file"></p>
      <p><input type="submit" value="Submit"></p>
    </form>
  </body>
</html>

Then your python code would look like this for uploading documents:
然后,您的Python代码将如下所示,用于上传文档:

from google.cloud import ndb, storage

from google.cloud.storage import Blob

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def upload_file():
    uploaded_file = request.files['file']
    if uploaded_file.filename != '':
        uploaded_file = request.files['file']       
        filename = "someUniqueFileName"
        client = storage.Client(project="yourProjectName")
        bucket = client.get_bucket("yourProjectName.appspot.com")
        blob = Blob(filename, bucket)
        blob.upload_from_file(uploaded_file)
    return redirect(url_for('index'))

Then to download files you need later you'd add this code where you want to download it from.
然后,要在以后下载文件,您需要在希望从中下载的位置添加以下代码。

bucket_name = "yourProjectName.appspot.com"
source_blob_name = "someUniqueFileName"
destination_file_name = "local/path/to/file"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)

If you have any more questions about this just let me know.
如果您对此有更多问题,只需让我知道。

英文:

For Google Cloud Storage you can use python and then use Flask as your web framework and then use this code to upload documents:

Your index should look like this

&lt;!doctype html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;File Upload&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;File Upload&lt;/h1&gt;
    &lt;form method=&quot;POST&quot; action=&quot;&quot; enctype=&quot;multipart/form-data&quot;&gt;
      &lt;p&gt;&lt;input type=&quot;file&quot; name=&quot;file&quot;&gt;&lt;/p&gt;
      &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;&lt;/p&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

Then your python code would look like this for uploading documents:

from google.cloud import ndb, storage

from google.cloud.storage import Blob

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route(&#39;/&#39;)
def index():
    return render_template(&#39;index.html&#39;)

@app.route(&#39;/&#39;, methods=[&#39;POST&#39;])
def upload_file():
    uploaded_file = request.files[&#39;file&#39;]
    if uploaded_file.filename != &#39;&#39;:
        uploaded_file = request.files[&#39;file&#39;]       
        filename = “someUniiqueFileName”
        client = storage.Client(project=“yourProjectName”)
        bucket = client.get_bucket(&quot;yourProjectName.appspot.com&quot;)
        blob = Blob(filename, bucket)
        blob.upload_from_file(uploaded_file)
    return redirect(url_for(&#39;index&#39;))

Then to download files you need later you'd add this code where you want to download it from.

bucket_name = &quot;yourProjectName.appspot.com&quot;
&#160; 
source_blob_name = &quot;someUniiqueFileName&quot;
&#160; 
destination_file_name = &quot;local/path/to/file&quot;

storage_client = storage.Client()

bucket = storage_client.bucket(bucket_name)

blob = bucket.blob(source_blob_name)

blob.download_to_filename(destination_file_name)

If you have any more questions about this just let me know.

huangapple
  • 本文由 发表于 2023年2月8日 20:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386096.html
匿名

发表评论

匿名网友

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

确定