Signed URL for video upload causing problem in GCP

549 Views Asked by At

I am working on a project in which I need to upload the video files to GCS bucket using V4 Signed URL. Currently I am generating the signed url using Python script which is a part of Flask API. Here is the method signature I am using to generate url.

def GenerateURL(self,bucket_name,blob_name,method,timeout,content_type=None):
    bucket = StoreCon.get_con(bucket_name)
    blob = bucket.blob(blob_name)
    url = blob.generate_signed_url(
        version="v4",
        expiration=datetime.timedelta(minutes=timeout),
        method=method,
        content_type=content_type,
        )

    resp = jsonify({'message':{'%s URL'%method:url}})
    resp.status_code = 200
    return resp

Now this is being called inside a blueprint route. Here is the snippet:

@CloudStoreEnd.route('/uploadMedia',methods=['POST'])
def uploadMedia():
    blob_name = request.get_json()['FILE_NAME']
    return StoreOperator.postMediaURL(blob_name)

When I make the call to this API route using Client side code, the video files are getting uploaded successfully to GCS bucket. But when I download the same video file from GCS bucket. The file becomes corrupted. Mentioning "0xc00d36c4" error. Here is a sample function for client side:

def upload_file(path):
  file_name = path.split('\\')[-1]
  data = {'FILE_NAME':file_name}

  #GET SIGNED URL FOR MEDIA UPLOAD
  get_signed_url = 'https://CLOUD-RUN-SERVICE/uploadMedia'
  headers = {'Content-Type':'application/json'}
  resp = requests.post(url=get_signed_url,data=json.dumps(data),headers=headers)
 upload_url = json.loads(resp.content)['message']['PUT URL']
 
 #SEND A PUT REQUEST WITH MEDIA FILE
 headers = {'Content-Type':MimeTypes().guess_type(file_name)[0]}
 file = {'file':open(path,'rb')}
 resp = requests.put(url=upload_url,headers=headers,files=file)
 return resp

I am not sure why the Media(.mp4,.mov) are getting corrupted when I retrieve the same files, whereas for other files like (.pdf,.png) the files are fine. Is there an extra request parameter I need to add to get proper signed url? Or from client application I am sending the files wrong way to the signed url?

0

There are 0 best solutions below