Be2
Be2

Reputation: 99

How to check which path the file landed in Cloud Storage folders using Cloud Function

I have a crontab job that gets new uploaded files to FTP and stores them in different folders in a Cloud Storage bucket as follows: if the file is sent by user1 it lands in bucket/user1/file.csv and so on for the rest of the users.

The next step is that I want to work on every file that comes with Cloud Function that applies changes on the new file in accordance to what path the new file landed in and saves it in another bucket. Knowing that Cloud Function can be triggered only with Storage bucket, I wanted to include an IF statement that checks if the new file landed in user1 folder then do function1 elseif user2 then function2 and so on.

However, I was not able to work it out code-wise until now.

Upvotes: 1

Views: 1514

Answers (1)

Mayeru
Mayeru

Reputation: 1094

Inside the Cloud Function you have scope to the "event" object, which has an "id" property.

This id is conformed as:

'bucket_name/folder_or_subfolder_name/newly_created_or_updated_object_name/object_generated_id'

So if you just cut the last two sections you will get the path where the object is located, you can also isolate the folder that is right above the object.

You can also declare other functions and reference to then accordingly depending on the conditions, just be sure that when creating the Cloud Function the "function to execute" field have the "main" function in it (in this case would be "hello_gcs".

Here's a snipped with an example in Python3:

def hello_gcs(event, context):
    """Triggered by a change to a Cloud Storage bucket.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    word = "dir-name"

    path = event["id"].rsplit("/",2) 
    parent_folder = event["id"].rsplit("/",3) 

    path = path[0]
    parent_folder = parent_folder[1]

    if parent_folder == word:
        one()
    else:
        two()


def one():
    print(f"You have entered function one")


def two():
    print(f"You have entered function two")

You can manipulate this more to fit your use case.

Upvotes: 1

Related Questions