简体   繁体   English

谷歌云存储上传方法返回签名的 URL。 Django python

[英]Google Cloud storage upload method is returning signed URL. Django python

This is my upload image method这是我上传图片的方法

from storages.backends.gcloud import GoogleCloudStorage

storage = GoogleCloudStorage()


def upload_image(file, dir_name, filename):
    try:
        target_path = '/static/images/' + dir_name + '/' + filename
        path = storage.save(target_path, file)
        return storage.url(path)
    except Exception as e:
        print(e)

This is my settings in the django settings.py这是我在 django settings.py 中的设置

DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'bucket name'
GS_PROJECT_ID = "project id"

GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    os.path.join(BASE_DIR,'google_cloud_credientials.json')
)

I have used this tutorial for reference https://medium.com/@mohammedabuiriban/how-to-use-google-cloud-storage-with-django-application-ff698f5a740f我已使用本教程作为参考https://medium.com/@mohammedabuiriban/how-to-use-google-cloud-storage-with-django-application-ff698f5a740f

This is returning me the signed URL for the file which I have uploaded and expired after some time, but I want to have a public URL that will be available for any time.这将返回我已签名的 URL 文件,该文件已上传并在一段时间后过期,但我想要一个公共的 URL 随时可用。

I want to have a public URL that will be available for any time.我想要一个随时可用的公共 URL。

Your only option for a URL that does not expire is to make the object public and use a Google Cloud Storage endpoint in this format:对于不会过期的 URL,您唯一的选择是公开 object 并使用以下格式的 Google Cloud Storage 端点:

https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME

Refer to this document:请参阅此文档:

Google Cloud Storage: Make data public 谷歌云存储:公开数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM