Blob被创建但文件未被使用SignedURL上传。
创始人
2024-12-22 19:00:10
0

在使用SignedURL上传文件时,如果创建了Blob对象但文件未被使用SignedURL上传,可能是因为以下原因:

  1. 未正确配置Blob的上传路径或属性。
  2. 未正确生成SignedURL或未正确使用SignedURL进行文件上传。

下面是一个示例代码,展示了如何解决这个问题:

from google.cloud import storage
from google.auth import compute_engine

def create_signed_url(bucket_name, blob_name):
    # 创建Blob对象
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    # 设置Blob的属性
    blob.content_type = 'application/octet-stream' # 设置文件类型
    blob.cache_control = 'public, max-age=3600' # 设置缓存控制
    blob.metadata = {'custom-key': 'custom-value'} # 设置自定义元数据

    # 生成SignedURL
    credentials = compute_engine.Credentials()
    url = blob.generate_signed_url(
        version='v4',
        credentials=credentials,
        expiration=datetime.timedelta(minutes=15),
        method='PUT'
    )

    return url

def upload_file_with_signed_url(url, file_path):
    # 使用SignedURL上传文件
    with open(file_path, 'rb') as file:
        response = requests.put(url, data=file)

    if response.status_code == 200:
        print('文件上传成功')
    else:
        print('文件上传失败')

# 示例使用
bucket_name = 'your-bucket-name'
blob_name = 'your-blob-name'
file_path = 'path/to/file'

signed_url = create_signed_url(bucket_name, blob_name)
upload_file_with_signed_url(signed_url, file_path)

在上面的示例代码中,create_signed_url函数用于创建Blob对象并生成SignedURL,upload_file_with_signed_url函数用于使用SignedURL上传文件。确保将bucket_nameblob_namefile_path替换为实际的值。

这样,你就可以正确地创建Blob对象,并使用SignedURL将文件上传到Google Cloud Storage中了。

相关内容

热门资讯

第6分钟了解(德州ai)外挂智... 第6分钟了解(德州ai)外挂智能ai辅助代打(透视)攻略教程(2023已更新)(哔哩哔哩);相信小伙...
第2分钟了解(tritonpo... 第2分钟了解(tritonpoker)外挂辅助插件安装代打(透视)黑科技教程(2024已更新)(哔哩...
第9分钟了解(WPK技巧)黑科... 第9分钟了解(WPK技巧)黑科技透明挂辅助软件(透视)教你攻略(2024已更新)(哔哩哔哩)是一款可...
七分钟了解(aapoker德州... 七分钟了解(aapoker德州俱乐部)软件透明挂辅助科技(透视)线上教程(2022已更新)(哔哩哔哩...
1分钟了解(Wepoke开发)... 1分钟了解(Wepoke开发)黑科技透明挂辅助黑科技(透视)扑克教程(2021已更新)(哔哩哔哩);...
第六分钟了解(wpk德州ai机... 第六分钟了解(wpk德州ai机器人)外挂辅助插件安装科技(透视)规律教程(2024已更新)(哔哩哔哩...
第1分钟了解(Wepoke技术... 第1分钟了解(Wepoke技术)外挂透明挂辅助下载(透视)实用技巧(2023已更新)(哔哩哔哩);1...
第十分钟了解(新版Wepoke... 第十分钟了解(新版Wepoke)软件透明挂辅助app(透视)曝光教程(2020已更新)(哔哩哔哩);...
第3分钟了解(wepOke)外... 第3分钟了解(wepOke)外挂辅助插件安装app(透视)第三方教程(2021已更新)(哔哩哔哩),...
八分钟了解(德扑之星设置)外挂... 八分钟了解(德扑之星设置)外挂辅助插件安装黑科技(透视)AI教程(2021已更新)(哔哩哔哩);人气...