小编Shr*_*rey的帖子

如何从 Google Cloud Platform 存储下载文件

我正在阅读 Google 云存储的 python 文档,并成功创建了上传文件的方法,但是,我无法找到使用 blob 的 URL 下载文件的方法。我能够使用文件名下载文件,但这并不实用,因为用户可以上传具有相同名称的文件。该 blob 是私有的。我可以访问该 blob 的 URL,因此我想知道是否有办法使用此链接下载文件。

这是我的上传代码,效果很好:

def upload_blob(bucket_name, filename, file_obj):
    if filename and file_obj:
        storage_client = storage.Client()
        bucket = storage_client.bucket('example-storage-bucket')
        blob = bucket.blob(filename)
        blob.upload_from_file(file_obj) # binary file data
        form_logger.info('File {} uploaded'.format(filename))
        return blob
Run Code Online (Sandbox Code Playgroud)

此代码下载文件,但我只能通过 blob 名称来计算,而不是 URL:

def download_blob(bucket_name, url):
    if url:
        storage_client = storage.Client()
        bucket = storage_client.bucket('example-storage-bucket')
        blob = bucket.blob(url)
        blob.download_to_filename("example.pdf")
Run Code Online (Sandbox Code Playgroud)

关于如何使用 blob 的媒体链接 URL 下载文件有什么建议或想法吗?

python google-cloud-storage google-cloud-platform

6
推荐指数
1
解决办法
8526
查看次数