Google云存储API没有上传blob到bucket



我一直遵循谷歌云存储文档,将服务器上的本地文件作为blob上传到我创建的桶中,但它不起作用。但是,它可以通过云控制台gui手动工作。请注意,我的代码用于从cloudstoragesink中的存储桶下载导出生成的对象,它也调用云存储API,但上传不起作用。我试图获得一些错误信息,以调试标准输出或标准错误,但无济于事。我的凭据/身份验证全部签出,因为我可以使用它们进行下载。此外,我试图上传的桶已经在另一个函数调用中创建,可能是存储客户端桶方法试图实例化一个已经存在的桶,因此在去上传blob方法之前没有得到过去并出错?如有任何见地,不胜感激。

从文件名函数上传blob的Py引用

上传对象到桶的GCS api指南

def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The path to your file to upload
# source_file_name = "local/path/to/file"
# The ID of your GCS object
# destination_blob_name = "storage-object-name"
storage_client = storage.Client.from_service_account_json('/opt/gws/creds/gws-vault-data-export-ops-bfd51297e810.json')
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)
print('n')
print('************List Blobs***********')
blobs_list = list_blobs(bucket_name_targ)
print('n')
print(blobs_list)
print('n')
blobs_list_set = set(map(lambda blob: blob.name, blobs_list))
print(blobs_list_set)
print('n')
dl_dir = export['name']
blob_file = export_filename_dl
source_file_name = os.path.join(working_dir_param, dl_dir, blob_file)
print(source_file_name)
destination_blob_name = blob_file + 'api-upload'
upload_blob(bucket_name_targ, source_file_name, destination_blob_name)
#if blob_file not in blobs_list_set:
#print('AT UPLOAD CONDITION BLOCK')
#upload_blob(bucket_name_targ, source_file_name, destination_blob_name)
#else:
#print('Upload did not succeed')

我让它工作了。事实证明,由于我使用的是带有字段参数的自定义可见模块,其中包括用于Linux pwd的字段参数,用于上传函数传递字符串路径的导出的下载,因此我必须正确地将自定义模块参数与下载的目录/文件路径相结合以传递到source_file_name。像这样:

"pwd": {"default": None, "type": "str"}
working_dir_param = module.params["pwd"]
path_tail_actual = directory_main + filename_main
source_file_name_actual = working_dir_param + path_tail_actual

最新更新