Adrian Humphrey
Adrian Humphrey

Reputation: 450

How do you post video data from ios to Signed Url to Google Cloud Bucket?

I have a python method that successfully creates a GET Signed Url that will download the video that is in the Google Cloud Bucket.

def _MakeUrlForApp(self, verb, path, content_type='', content_md5=''):
    """Forms and returns the full signed URL to access GCS."""
    base_url = '%s%s' % (self.gcs_api_endpoint, path)
    signature_string = self._MakeSignatureString(verb, path, content_md5,
                                                 content_type)
    signature_signed = self._Base64Sign(signature_string)

    """replace @ with %40 - and + with %2 and == with %3D"""
    signature_signed = signature_signed.replace("+", "%2B")
    signature_signed = signature_signed.replace("/", "%2F")
    signature_signed = signature_signed.replace("=", "%3D")
    self.client_id_email = self.client_id_email.replace("@", "%40")

    signedURL = base_url + "?Expires=" + str(self.expiration) + "&GoogleAccessId=" + self.client_id_email + "&Signature=" + signature_signed 
    print 'this is the signed URL '
    print signedURL
    return signedURL

This is called in ios swift with a get post with http. It returns the signed url and it downloads the video to the ios app.

This method here, If i specify the bucketname, the objectname, text/plain as content type, and a couple words for the data, It creates and puts that file into the Google Cloud bucket for me.

    def Put(self, path, content_type, data):
    """Performs a PUT request.
    Args:
      path: The relative API path to access, e.g. '/bucket/object'.
      content_type: The content type to assign to the upload.
      data: The file data to upload to the new file.
    Returns:
      An instance of requests.Response containing the HTTP response.
    """
    md5_digest = base64.b64encode(md5.new(data).digest())

    base_url, query_params = self._MakeUrl('PUT', path, content_type,
                                           md5_digest)
    headers = {}
    headers['Content-Type'] = content_type
    headers['Content-Length'] = str(len(data))
    headers['Content-MD5'] = md5_digest
    return self.session.put(base_url, params=query_params, headers=headers,
                            data=data)

What I want to know is one of these two things and nothing else. How do I upload data from a video to this data parameter in my python webapp2.requestHandler from ios? OR How do I get the correct put signed Url to upload video data?

Please do not comment with anything that will not solve this specific question and do not bash me for my methods. Please provide suggests that you feel will specifically help me and nothing else.

Upvotes: 1

Views: 893

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38389

There are a few ways to upload images to GCS, and each way works with signed URLs. If the video files are small, your simplest option is to have the users perform a non-resumable upload, which has the same URL signature except that the verb is PUT instead of GET. You'll also need to add the "Content-Type" header to the signature.

Video files can be fairly large, though, so you may prefer to use resumable uploads. These are a bit more complicated but do work with signed URLs as well. You'll need to use the "x-goog-resumable: start" header (and include it in the signature) and set "Content-Length" to 0. You'll get back a response with a Location header containing a new URL. Your client will then use that URL to do the upload. Only the original URL needs to be signed. The client can use the followup URL directly.

Upvotes: 1

Related Questions