How to upload a file programmatically to GCS

In my app, users can select local files and upload them to GCS using a resource query.

I'm trying to add an option to upload instead of from the local filesystem from the cloud. I have an API call that returns the url of the generated image.

I can't figure out a way to pass the url or binary to the resource query.

Looks like I can't set a File object in a variable. I also read that I can't set the file of an input file component. I also can't have async transformations (url -> file). I can't define additional scope variables from my resource query.

Any ideas how this could be done? Thanks! :pray:

Hey @Christian_G, if you have the url of the file, then in general you can use a GET REST API query to retrieve the binary of the file to pass to the upload query.

Step 1 of this reply here should show you how to get it.

Let me know if that doesn't work for you.

Hi Joe, downloading the URL to a file is easy, the problem is the upload. How can I pass it to the query without using the client file selector? Can I upload it to GCS as base64 string? :thinking:

@Christian_G They type of upload / content type can be set depending on the data you are trying to upload. If you want, you can supply the file object returned from the REST API query to the GCS upload query.

GET REST API:

GCS Upload:

You'll notice that the file object returned by the REST API query is the same as a file object from a file uploaded using the client file selector.

REST data:
CleanShot 2024-06-25 at 13.16.48@2x

File Selector:
CleanShot 2024-06-25 at 13.16.31@2x

Let me know if you have any other questions.

If you want to change any of the file object information, you can do so by creating an intermediate JS query just like in the other post.

In my case, I have a button that fetches the image and sets a temporary variable. Thanks to you I could set such variable to emulate the file selector interface. Then I reuse the same upload query which checks if to use the temporary one or the selected file. But the image uploaded is corrupted and it is always 15 Bytes.

GCS upload:

Download script:

Any idea what could be wrong? :thinking: Thanks! :pray:

The 15Bytes file seems to be a txt file containing "[object Object]" :smiley: