Google cloud storage API not uploading blob to bucket

Viewed 368

I have adhered to google cloud storage documentation for getting a local file on a server uploaded as a blob to a bucket I created yet it does not work. It does work manually via the cloud console gui however. Note that my code for downloading an export generated object from a bucket in a cloudstoragesink works which does also call the cloud storage API, but the upload does not. I have tried to get some errors info for debugging on stdout or stderr to no avail. My credentials/authentication all checks out as I am able to use them for downloading. Also, the bucket I am trying to upload to has already been created in another function call, could it be that the storage client bucket method is attempting to instantiate an already existing bucket, thus not getting past that and erroring out before going to the upload blob method ? Any insight is appreciated.

Py reference for blob upload from filename function

GCS api guide for uploading objects to bucket

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')
1 Answers

I got it working. Turns out, since I am using a custom ansible module with field parameters including one for Linux pwd for the downloads of the exports which the upload function gets passed the string path for, I had to correctly combine the custom module parameter with the downloaded directory/file path to pass into source_file_name . Like so:

"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
Related