用Djang将文件发送到GCS的最佳方式

2024-10-06 11:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

用django将文件发送到Google云存储的最佳方式是什么。在

现在我用post方法获取文件

file = request.FILES['image']

但当我试着去GCS的时候

^{pr2}$

我得到了这个错误InMemoryUploadedFile,所以我必须将文件保存在一个临时文件中,然后发送到GCS,但是速度很慢。在


Tags: 文件django方法imagerequest错误google方式
1条回答
网友
1楼 · 发布于 2024-10-06 11:21:36

错误可能是由于文件名和文件名的错误使用。尝试使用上载请求同步文件流,如下所示:

from google.cloud import storage

def upload_file(file,projectName,bucketName, fileName, content_type):

    client = storage.Client(projectName)
    bucket = client.get_bucket(bucketName)
    blob = bucket.blob(fileName)

    blob.upload_from_string(
        file,
        content_type=content_type)

    url = blob.public_url
    if isinstance(url, six.binary_type):
        url = url.decode('utf-8')

    return url

进一步阅读,Reference.

相关问题 更多 >