Reputation: 79
As the title says, I need to fetch the size of the video / object I just uploaded to the bucket.
Every few seconds, an object is uploaded to my bucket which is of the form, {id}/video1.mp4
.
I want to make use of google cloud storage triggers which would alert me if a 0 byte video was added. Can someone pls suggest me how to access the size of the added object.
Upvotes: 0
Views: 559
Reputation: 159
Farhan,
Assuming you know the basics of cloud functions. You can create a cloud function trigger that runs a script every-time you create/finalize an object in a selected bucket. The link you posted contains the tutorial and the following python script attached.
def hello_gcs_generic(data, context):
"""Background Cloud Function to be triggered by Cloud Storage.
This generic function logs relevant data when a file is changed.
Args:
data (dict): The Cloud Functions event payload.
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the output is written to Stackdriver Logging
"""
print('Event ID: {}'.format(context.event_id))
print('Event type: {}'.format(context.event_type))
print('Bucket: {}'.format(data['bucket']))
print('File: {}'.format(data['name']))
print('Metageneration: {}'.format(data['metageneration']))
print('Created: {}'.format(data['timeCreated']))
print('Updated: {}'.format(data['updated']))
In this example, we see data has multiple items such as name, timeCreated ect.
What this example doesn't show however is that data has another item, SIZE!
listed as data['size']
So now we have a cloud function that gets the filename, and file size of whatever is uploaded when it's uploaded!. all we have to do now is create an if statement to do "something" if the file size is = 0. It will look something like this in python. (apologies for syntax issues, but this is the jist of it)
def hello_gcs_generic(data, context):
"""Background Cloud Function to be triggered by Cloud Storage.
This generic function logs relevant data when a file is changed.
Args:
data (dict): The Cloud Functions event payload.
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the output is written to Stackdriver Logging
"""
print('File: {}'.format(data['name']))
print('Size: {}'.format(data['size']))
size = data['size']
if size == 0:
print("its 0!")
else:
print("its not 0!")
Hope this helps!
Upvotes: 3