0

I am trying to create a Google Function with Python that will accept variables and upload a file to a stoage bucket. Here is the code to upload, below it is the error I'm getting. I have double checked the path and filename that I am sending via the function URL, but it's not working. The URL is similar to this: https://us-central1-gcpdemo.cloudfunctions.net/uploaddemo?pathtofile=c:\Temp\&blobname=IMG_3014.PNG&bucketname=gcpingestionbucketdemo

from google.cloud import storage

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """

    #add this lines at top
    path_to_file = request.args.get('pathtofile')
    blob_name = request.args.get('blobname')
    #gcpingestionbucketdemo
    bucket_name = request.args.get('bucketname')

    #define client
    # use service account credentials .
    # storage_client = storage.Client.from_service_account_json('creds.json')
    
    storage_client = storage.Client()
    #define bucket
    bucket = storage_client.get_bucket(bucket_name)
    
    #Blob: File name that will be saved.
    blob = bucket.blob(blob_name)
    blob.upload_from_filename(path_to_file)

textPayload: "Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 402, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 261, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 30, in hello_world blob.upload_from_filename(path_to_file) File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 2458, in upload_from_filename with open(filename, "rb") as file_obj: FileNotFoundError: [Errno 2] No such file or directory: 'c:\Temp\'

  • You are trying to upload a file from 'c:\Temp\', however Cloud Functions doesn't run over Windows and there is no local disk C. Where exactly is the source file that you are trying to upload to storage? Give us more info aobut the overall workflow please – Chris32 Dec 14 '20 at 19:47
  • 1
    Oh, thanks for letting me know about that! I need to create a way to upload a file into a Google Storage bucket through a URL way. Then once the file is uploaded, I would do additional work on it while it resides in the bucket. How can I upload the file through a browser properly into a bucket? I'm very new to GCP and Python so I'm not sure. – Christian Palacios Dec 14 '20 at 20:34
  • This [thread](https://stackoverflow.com/questions/54235721/transfer-file-from-url-to-cloud-storage) may help you understand why it is not possible to upload a file to Google Cloud Storage directly from a URL. Please elaborate and update your post why you want to upload a local file to Cloud Storage using Cloud Functions. Usually it's easier to upload local files to GCS using [`gsutil`](https://cloud.google.com/storage/docs/uploading-objects#gsutil) command – Donnald Cucharo Dec 15 '20 at 07:51
  • You cannot do additional work on an object in Cloud Storage that modifies the content. Objects are immutable. You would need to download it, modify and then upload. – John Hanley Dec 15 '20 at 07:56
  • Thank you for the explanation. When I mentioned additional work to the file I upload, I'm talking about grabbing some metadata from it, while it's in the GCP bucket, is that possible? I'm trying to test uploading a LAS file and then getting some information from it. Here is a description of the file. https://desktop.arcgis.com/en/arcmap/10.3/manage-data/las-dataset/what-is-a-las-dataset-.htm#:~:text=A%20LAS%20file%20is%20an,contained%20in%20the%20LAS%20files. Once it's uploaded, I'm hoping to extract details from that file, which in turn I can then upload to a MongoDB as a document. – Christian Palacios Dec 15 '20 at 17:16
  • As well, should I be using an API then to upload the file? I am not very familiar with APIs as well, but I know they have a URL, could I use that URL instead?? Sorry for all the questions, I'm kinda at a loss right now. – Christian Palacios Dec 15 '20 at 17:17
  • Please update your post with your objectives that you mentioned in the comments. Also, let us know if you find these links relevant to your issue: https://cloud.google.com/storage/docs/uploading-objects#rest-upload-objects https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-python – Donnald Cucharo Dec 16 '20 at 02:54

0 Answers0